Translating from Mathematica to Matlab

In summary: And finally, as a general rule, try to avoid i and j as variable names in matlab. They are the built-in imaginary unit, and if you have a line likez=z+sqrt(i)it will probably not do what you want, as you will not get a complex number out of sqrt(i). In this case it works, because z is a vector of real numbers and sqrt(i) is a scalar, but it's a bad habit to be in. Also, if you ever try to move to python or similar languages, i and j are not preassigned, so you'll get errors right away.
  • #1
Frank Einstein
170
1
Hello everyone.

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}
vals is a eigenvalues vector, vecs is a eigenvector matrix and z is a vector of random variables of length 10

Thanks for your answer.
 
Physics news on Phys.org
  • #2
Why not decompose it into simpler expressions and then convert them yourself?

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

I spaced it out for readability in your post.
 
  • Like
Likes Frank Einstein
  • #3
jedishrfu said:
Why not decompose it into simpler expressions and then convert them yourself?

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

I spaced it out for readability in your post.

Indeed, I forgot to close the right square bracket.

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
My take is:

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

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?
 
  • Like
Likes Frank Einstein
  • #5
jedishrfu said:
My take is:

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

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?

Thank you very much for your answer. I will keep working on it.
 
  • #6
vecs[[j, all]] should translate to vecs(j,:) or vecs(:,j). the semi-colon tells MATLAB to select all of the elements in that dimension (j,:) being everything in the jth row, and (:,j) being everything in the jth column.

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
Hepth said:
and so taking one row or column leaves it as an array, and so you need array operations like .*, ./ etc.
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].

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

for i=1:long
z(i)=normrnd(0,1);
f(i)=0;
endfor 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);
The first sqrt(vals.*z) is an N array containing sqrt(vals(j)*z(j)) in element j. The second sqrt contains the square root elements of vecs, which should also go into the sum. The matrix multiplication performs the sum. You may need to add primes on vals and z to get them to row vector format and you may need a prime on vecs if it is the other index you want to contract with.
 
  • #8
Frank Einstein said:
z(i)=normrnd(0,1);
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:
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.
so
Code:
z = randn(1,N);
will do perfectly fine to get a row vector with N numbers distributed according to ##\mathcal N(0,1)##.

Frank Einstein said:
f(i)=0;
Likewise, this should not be in a for loop. It should be
Code:
f = zeros(1,N);
 

Related to Translating from Mathematica to Matlab

1. What are the main differences between Mathematica and Matlab?

Mathematica and Matlab are both mathematical computation software, but they have different programming languages and features. Mathematica uses symbolic manipulation while Matlab uses numerical computation. Additionally, Matlab is primarily used for matrix manipulation and data analysis, while Mathematica has a wider range of applications including machine learning and data visualization.

2. Can I easily convert my Mathematica code to Matlab?

No, it is not a simple process to convert code from Mathematica to Matlab. The two languages have different syntax and functionality, so a direct conversion is not possible. However, there are some tools and techniques that can assist in the conversion process, such as using the "ToMatlab" function in Mathematica or manually rewriting the code in Matlab.

3. Will my Mathematica code run faster in Matlab?

It is difficult to say whether your code will run faster in Matlab without testing it. While Matlab is generally known for its efficient matrix operations, Mathematica also has its own optimizations and parallel processing capabilities. It is recommended to benchmark your code in both languages to determine which is faster for your specific application.

4. Are there any limitations when translating from Mathematica to Matlab?

Yes, there are some limitations to consider when converting code from Mathematica to Matlab. Some functions and operations may not have direct equivalents in the other language, and certain advanced features in Mathematica may not be available in Matlab. It is important to carefully review your code and consider any potential limitations before attempting to translate it.

5. Can I translate code from Matlab to Mathematica?

Yes, it is possible to translate code from Matlab to Mathematica, but it may also require some manual rewriting. The two languages have different syntax and functionality, so a direct translation is not always possible. There are also tools and techniques available to assist in the conversion process, such as using the "ToMatlab" function in Mathematica or using the "matlabFunction" command in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
895
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Back
Top