- #1

- 157

- 1

I was wondering if someone could tell me how to write the next line of Mathematica code in Matlab.

Code:

`x = Sum [ Sqrt [ vals[ [ j ] ] ] * vecs [ [ j, All ] ] * z [ [ j ] ] , {j,1,10}`

Thanks for your answer.

- Mathematica
- Thread starter Frank Einstein
- Start date

- #1

- 157

- 1

I was wondering if someone could tell me how to write the next line of Mathematica code in Matlab.

Code:

`x = Sum [ Sqrt [ vals[ [ j ] ] ] * vecs [ [ j, All ] ] * z [ [ j ] ] , {j,1,10}`

Thanks for your answer.

- #2

jedishrfu

Mentor

- 12,505

- 6,293

Also it looks like you might be missing a square bracket or something.

I spaced it out for readability in your post.

- #3

- 157

- 1

Indeed, I forgot to close the right square bracket.

Also it looks like you might be missing a square bracket or something.

I spaced it out for readability in your post.

Believe me. i have tried to do it myself. That code is from a colleague and represents the generation of a random process with a Karhunen Loeve expansion.

My attempts have not given propper results, this is my code:

for i=1:long

z(i)=normrnd(0,1);

f(i)=0;

end

for i=1:long%times

for j=1:long%number of terms in the descomposition

f(i)=f(i)+sqrt(eigenvalues2(j))*z(j)*eigenvectors(i,j);

end

end

However, since this hasn't worked, I have tried to do it manually:

% f5(1)=eigenvectors(1,5)*sqrt(eigenvalues(5))*z(5)+eigenvectors(1,4)*sqrt(eigenvalues(4))*z(4)+eigenvectors(1,3)*sqrt(eigenvalues(3))*z(3)+eigenvectors(1,2)*sqrt(eigenvalues(2))*z(2)+eigenvectors(1,1)*sqrt(eigenvalues(1))*z(1);

% f5(2)=eigenvectors(2,5)*sqrt(eigenvalues(5))*z(5)+eigenvectors(2,4)*sqrt(eigenvalues(4))*z(4)+eigenvectors(2,3)*sqrt(eigenvalues(3))*z(3)+eigenvectors(2,2)*sqrt(eigenvalues(2))*z(2)+eigenvectors(2,1)*sqrt(eigenvalues(1))*z(1);

% f5(3)=eigenvectors(3,5)*sqrt(eigenvalues(5))*z(5)+eigenvectors(3,4)*sqrt(eigenvalues(4))*z(4)+eigenvectors(3,3)*sqrt(eigenvalues(3))*z(3)+eigenvectors(3,2)*sqrt(eigenvalues(2))*z(2)+eigenvectors(3,1)*sqrt(eigenvalues(1))*z(1);

% f5(4)=eigenvectors(4,5)*sqrt(eigenvalues(5))*z(5)+eigenvectors(4,4)*sqrt(eigenvalues(4))*z(4)+eigenvectors(4,3)*sqrt(eigenvalues(3))*z(3)+eigenvectors(4,2)*sqrt(eigenvalues(2))*z(2)+eigenvectors(4,1)*sqrt(eigenvalues(1))*z(1);

% f5(5)=eigenvectors(5,5)*sqrt(eigenvalues(5))*z(5)+eigenvectors(5,4)*sqrt(eigenvalues(4))*z(4)+eigenvectors(5,3)*sqrt(eigenvalues(3))*z(3)+eigenvectors(5,2)*sqrt(eigenvalues(2))*z(2)+eigenvectors(5,1)*sqrt(eigenvalues(1))*z(1);

However, if I calculate the correlation matrix for some realizations of this, the covariance matrix doesn't look like the kernel at all.

So I don't know what I should try next.

- #4

jedishrfu

Mentor

- 12,505

- 6,293

Matlab:

```
for j = 1:10
x=vals(j)*vecs(j)*z(j)
sum=sum+sqrt(x)
end
```

- #5

- 157

- 1

Thank you very much for your answer. I will keep working on it.

I don’t know what the vecs[[j,all]] that’s something you’ll have to get from Mathematica. it looks to be related to matrix multiply against the z vector?Matlab:`for j = 1:10 x=vals(j)*vecs(j)*z(j) sum=sum+sqrt(x) end`

- #6

Hepth

Gold Member

- 448

- 39

So something like

sum = [];

for j = 1:10

sum = sum + sqrt(vals(j) .* vecs(:,j) .* z(j)) ;

end

the .* means to multiply element-wise ( so 2 .* [1 1 1] becomes [2 2 2])

vecs is a matrix, and so taking one row or column leaves it as an array, and so you need array operations like .*, ./ etc.

- #7

- 16,829

- 6,652

In this particular case, you do not need them. The only thing with elements in your expression is vecs(:,j), vals(j) and z(j) are both numbers. 2*[1 1 1] also evaluates to [2 2 2].and so taking one row or column leaves it as an array, and so you need array operations like .*, ./ etc.

My attempts have not given propper results, this is my code:

for i=1:long

z(i)=normrnd(0,1);

f(i)=0;

end

for i=1:long%times

for j=1:long%number of terms in the descomposition

f(i)=f(i)+sqrt(eigenvalues2(j))*z(j)*eigenvectors(i,j);

end

end

In general, you should try to avoid for loops in matlab. It was written to take care of matrix operations.

What OP wants to do, given that vals is a N length row vector, z is a N length row vector, and vecs is a NxN matrix is likely on the form:

Code:

`f = sqrt(vals.*z)*sqrt(vecs);`

- #8

- 16,829

- 6,652

Same thing with initialisations. There is no point in calling your random number generator multiple times. The first few lines in the randn function help read:z(i)=normrnd(0,1);

Code:

```
randn Normally distributed pseudorandom numbers.
R = randn(N) returns an N-by-N matrix containing pseudorandom values drawn
from the standard normal distribution. randn(M,N) or randn([M,N]) returns
an M-by-N matrix.
```

Code:

`z = randn(1,N);`

Likewise, this should not be in a for loop. It should bef(i)=0;

Code:

`f = zeros(1,N);`

- Last Post

- Replies
- 2

- Views
- 4K

- Replies
- 3

- Views
- 3K

- Replies
- 14

- Views
- 9K

- Last Post

- Replies
- 2

- Views
- 1K

- Last Post

- Replies
- 4

- Views
- 3K

- Last Post

- Replies
- 7

- Views
- 6K

- Replies
- 5

- Views
- 3K

- Last Post

- Replies
- 20

- Views
- 177K

- Last Post

- Replies
- 12

- Views
- 1K

- Last Post

- Replies
- 1

- Views
- 3K