Azzera filtri
Azzera filtri

matrix issue with null

1 visualizzazione (ultimi 30 giorni)
George
George il 19 Feb 2011
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

Risposta accettata

Matt Tearle
Matt Tearle il 19 Feb 2011
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 Commenti
Matt Tearle
Matt Tearle il 20 Feb 2011
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 il 20 Feb 2011
Ok,thanks a lot!I think sum(matrix(:)) is what i need!

Accedi per commentare.

Più risposte (2)

Matt Tearle
Matt Tearle il 19 Feb 2011
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 Commento
George
George il 19 Feb 2011
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

Accedi per commentare.


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

Accedi per commentare.

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by