How to translate the following code from Mathematica to Matlab?

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

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

OK, but that way is specific to that one specific matrix it generates, not a " general way to construct matrix from a function" like you asked. Given some equation, you can generate a specific matrix, like you did. The only thing general/generic about your code is that you could enclose all that code inside a function definition if you want, but the code inside the function definition will depend on what matrix needs to be generated. Anyway, good luck on your transition from Mathematica to MATLAB.
I see. I've edited my answer so it's clear that was a specific way. Thank you again.
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.
Sorry, it was my first time here so I got confused

Sign in to comment.

More Answers (3)

Try this:
c = 0:.5:2.5;
r = -c;
toeplitz(c,r)

3 Comments

Thank you for your promptly answer. What about this one?
a = 1; b = 100;
y = Table[1/(a ((b - 1) l)/((b + 1) l + 1))
KroneckerDelta[l, m], {l, 1, 5}, {m, 1, 5}] // MatrixForm
I'd like to figure out how a general way to construct matrix from a function.
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.
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.

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

toeplitz directly constructs it (his first example) in one line of code - did you see my answer?
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.
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.

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;

Asked:

on 30 Aug 2014

Edited:

on 2 Sep 2014

Community Treasure Hunt

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

Start Hunting!