How to translate the following code from Mathematica to Matlab?

6 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Eric
Eric il 1 Set 2014
Modificato: Eric il 1 Set 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 Commenti
Image Analyst
Image Analyst il 1 Set 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.
Eric
Eric il 1 Set 2014
Sorry, it was my first time here so I got confused

Accedi per commentare.

Più risposte (3)

Image Analyst
Image Analyst il 30 Ago 2014
Try this:
c = 0:.5:2.5;
r = -c;
toeplitz(c,r)
  3 Commenti
Image Analyst
Image Analyst il 30 Ago 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 il 31 Ago 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

Accedi per commentare.


Guillaume
Guillaume il 30 Ago 2014
Modificato: Guillaume il 30 Ago 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 Commenti
Guillaume
Guillaume il 30 Ago 2014
Modificato: Guillaume il 30 Ago 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 il 31 Ago 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.

Accedi per commentare.


Andrei Bobrov
Andrei Bobrov il 1 Set 2014
Modificato: Andrei Bobrov il 2 Set 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!

Translated by