Effacer les filtres
Effacer les filtres

matrix issue with null

1 vue (au cours des 30 derniers jours)
George
George le 19 Fév 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

Réponse acceptée

Matt Tearle
Matt Tearle le 19 Fév 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 commentaires
Matt Tearle
Matt Tearle le 20 Fév 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 le 20 Fév 2011
Ok,thanks a lot!I think sum(matrix(:)) is what i need!

Connectez-vous pour commenter.

Plus de réponses (2)

Matt Tearle
Matt Tearle le 19 Fév 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 commentaire
George
George le 19 Fév 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

Connectez-vous pour commenter.


Matt Fig
Matt Fig le 19 Fév 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 commentaire
George
George le 19 Fév 2011
Thanks for the help!The first problem worked.As for the outputs please see above.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by