How to translate the following code from Mathematica to Matlab?

6 views (last 30 days)
I need to construct a matrix similar to the following:
l = 2; m = 4; Table[(2 x - y*l)/m, {x, 0, 5}, {y, 0, 5}] // MatrixForm
How can I do that in Matlab?

Accepted Answer

Eric
Eric on 1 Sep 2014
Edited: Eric on 1 Sep 2014
Thank you all for the answers
I've found a generic (edit: specific) way through the following script (with the above example illustrated)
a=1; b=100;
format rat
y=zeros(5,5);
for l=1:5
for m=1:5
y(l,m)=1/(a*((b-1)*l)/((b+1)*l+1))*KroneckerDelta(l,m)
end;
end;
  4 Comments
Image Analyst
Image Analyst on 1 Sep 2014
You're welcome. By the way, I think I should have gotten the credit/points for the answer (the official "Accept") since I was the one who answered your original posted question. Oh well, maybe on your next question.

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 30 Aug 2014
Try this:
c = 0:.5:2.5;
r = -c;
toeplitz(c,r)
  3 Comments
Image Analyst
Image Analyst on 30 Aug 2014
There is no way to construct a given matrix from a general purpose universal function. For example, this second matrix you gave will require a different function. If you specify a function, then you can build an output matrix. But if you specify an output matrix, then you're done. No need to find some function to generate it because you already have it.
Image Analyst
Image Analyst on 31 Aug 2014
Of course you can translate Mathematica code. You can learn how to write functions in MATLAB. I suggest you look at this http://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab

Sign in to comment.


Guillaume
Guillaume on 30 Aug 2014
Edited: Guillaume on 30 Aug 2014
There isn't a function in matlab to directly construct a matrix as in your examples but you could replicate it with meshgrid and arrayfun.
I don't know anything about mathematica but it looks like x and y (or m and l in your latest example) represent row and column inputs so:
l = 2; m = 4;
[y x] = meshgrid(0:5, 0:5);
t = arrayfun(@(xx, yy) (2*xx-yy*l)/m, x, y);
  3 Comments
Guillaume
Guillaume on 30 Aug 2014
Edited: Guillaume on 30 Aug 2014
Yes, but Tigo is asking for a generic way. Toeplitz doesn't work for the second example.
I'm answering his question in his comment to your answer but using his first example as I've no idea what kroneckerdelta is.
Image Analyst
Image Analyst on 31 Aug 2014
There's no one function that will generate any arbitrary matrix he happens to toss out there. He can write a function but the function will be whatever he says it will be. And if he tosses out some arbitrary Mathematica code, then it will have to be translated "custom" for whatever code he specifies.

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 1 Sep 2014
Edited: Andrei Bobrov on 2 Sep 2014
l = 2:10;
m = 1:5;
idx = bsxfun(@eq,l(:),m(:)');
MatrixForm = idx + 0;
k = intersect(l,m);
a = 1;
b = 100;
MatrixForm(idx) = 1./(a*((b - 1)*k)./((b + 1)*k + 1));
first answer:
Wolfram Mathematica:
l = 2;
m = 4;
Table[(2 x - y*l)/m, {x, 0, 5}, {y, 0, 5}] // MatrixForm
Matlab:
l = 2;
m = 4;
out = bsxfun(@minus,2*(0:5)',(0:5)*l)/m;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!