필터 지우기
필터 지우기

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