matrix issue with null

Hello, i have this code in mathematica and i want to do it in matlab:
T = Table[{Null, Null}, {Maxstep}]
t = Table[Null, {Maxstep}]
T2 = Table[{0, 0}, {Maxstep}]
How can i do it?(the null?) Thanks!
EDIT---->> Also,this : ml = Table[If[RandomReal[] < 0.5, 1, -1], {L}, {L}];
I tried this: ml=rand(L,L); if rand()<0.5 ml(:,:)==1 else ml(:,:)==-1 end ml
but it gives me random numbers 0-1 and i want only 1 and -1

 采纳的回答

If you want a 2-by-|Maxstep| table of nulls, you can do
T = NaN(2,Maxstep);
This way, T will be numeric. Otherwise, go with Matt Fig's suggestion of a cell array of empty arrays.

7 个评论

I want 1 line and 10 columns (Maxstep) with pairs of NaN.What you gave me creates 2 lines and no pairs.The 2 lines,ok i write 1 but how can i create the pairs Nan,Nan?
Well, before you do that, the bigger question is why? You see, MATLAB treats everything as a matrix, so things tend to work smoother if you can use that. For example, with the matrix I showed, you can easily extract the kth pair as a vector by extracting the kth row of the matrix:
x = T(k,:)
Leaving the pairs as a matrix allows you the ability to perform many operations on them (assuming they won't remain NaN forever).
But, if you really want a 1-D array of pairs, create a cell array, as Matt Fig showed, then populate each cell with NaN(1,2). Equivalently, you can use mat2cell to turn the matrix (that I showed) into a 1-by-Maxstep cell array.
Hello and thanks for helping.I found this "T= NaN(1,2,Maxstep)"whicj i think its what i want.For example ,i have :magnetT[[ii]] = {T, Apply[Plus, Flatten[mlat]]/L2
Also,if you could tell me sth for the above statement.If i have Apply[Plus,matrix] , i do "sum(matrix)".
I i have Apply[Plus,Flatten[matrix]] what can i do ? Thanks!
Again, I don't really know what the Mma command does, but I'm guessing what you're looking for is sum(matrix,3) and/or sum(reshape(matrix,...))
As the name suggests, reshape changes the dimensions of an array, so you could change a 3-D into a 2-D array (eg). Stats functions like sum accept optional dimension arguments, so sum(x,3) adds across the third index. In your case this would add up the Maxstep pairs (row vectors)
If you want the sum of everything, you can reshape into a 1-D array. A neat short-cut syntax for this is sum(matrix(:))
Ok,thanks a lot!I think sum(matrix(:)) is what i need!

请先登录,再进行评论。

更多回答(2 个)

Best practice is to make separate posts for separate questions. For the first question, can you explain what the desired output is? (I haven't used Mma in ages)
For the second question, your approach wasn't bad, but your if condition invoked rand a second time, then set all of ml to 1 or -1. Try rounding, in conjunction with shifting and scaling:
ml = 2*round(rand(L,L)) - 1;

1 个评论

Hello and thanks for the answer.It worked fine.As for the output of tables :
T={{Null, Null}, {Null, Null}, {Null, Null}, {Null, Null}, {Null,
Null}, {Null, Null}, {Null, Null}, {Null, Null}, {Null,
Null}, {Null, Null}} ,
t={Null, Null, Null, Null, Null, Null, Null, Null, Null, Null}
and T2={{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0,
0}, {0, 0}} ,where Maxstep=10

请先登录,再进行评论。

Since I don't have Mathematica, and many may not, can you show what the output of those commands is? That way we could try to match it in MATLAB. My best guess is you want something like this:
T = cell(1,Maxstep);
ml = rand(L,L);
ml(ml>.5) = 1;
ml(ml<=.5) = -1
Or all at once:
ml = round(rand(L,L))*2-1

1 个评论

Thanks for the help!The first problem worked.As for the outputs please see above.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by