How to translate the following code from Mathematica to Matlab?
Show older comments
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
More Answers (3)
Image Analyst
on 30 Aug 2014
Try this:
c = 0:.5:2.5;
r = -c;
toeplitz(c,r)
3 Comments
Eric
on 30 Aug 2014
Edited: Image Analyst
on 30 Aug 2014
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
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
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
Image Analyst
on 30 Aug 2014
toeplitz directly constructs it (his first example) in one line of code - did you see my answer?
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.
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;
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!