matrix issue with null

1 次查看(过去 30 天)
George
George 2011-2-19
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

采纳的回答

Matt Tearle
Matt Tearle 2011-2-19
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 个评论
Matt Tearle
Matt Tearle 2011-2-20
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(:))
George
George 2011-2-20
Ok,thanks a lot!I think sum(matrix(:)) is what i need!

请先登录,再进行评论。

更多回答(2 个)

Matt Tearle
Matt Tearle 2011-2-19
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 个评论
George
George 2011-2-19
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

请先登录,再进行评论。


Matt Fig
Matt Fig 2011-2-19
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 个评论
George
George 2011-2-19
Thanks for the help!The first problem worked.As for the outputs please see above.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by