Borrar filtros
Borrar filtros

matrix issue with null

1 visualización (últimos 30 días)
George
George el 19 de Feb. de 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

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (2)

Matt Tearle
Matt Tearle el 19 de Feb. de 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 comentario
George
George el 19 de Feb. de 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

Iniciar sesión para comentar.


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

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by