Asignar matriz automaticamente.

7 visualizzazioni (ultimi 30 giorni)
David Carrillo
David Carrillo il 3 Mag 2022
Hola Comunidad de Mathworks, les quería pedor ayuda acerca de como guardar distintas matrices en variables automaticamente.
Estoy intentando hacer lo siguiente:
a partir de una matriz M(:,:,:)
quiero generar n matrices llamadas M1,M2,M3, etc
donde cada Matriz corresponda a M(:,:,n)
estoy intentando aplicar el siguiente comando, pero solo me funciona con numeros, no con matrices.
for n=1:nf
eval(sprintf(' M%d = n ', Mm(:,:,n)))
end
De modo que quede como sigue:
M1=M(:,:,1)
M2=M(:,:,2)
...
Mn=M(:,:,n)
Agradezco desde ya su ayud.
Saludos cordiales!
  1 Commento
Stephen23
Stephen23 il 3 Mag 2022
Modificato: Stephen23 il 3 Mag 2022
"quiero generar n matrices llamadas M1,M2,M3, etc"
The MATLAB documentation states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
In fact your approach forces you into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug. Read this to know why:
It is very very unlikely that you need to do this anyway:

Accedi per commentare.

Risposte (1)

Constantino Carlos Reyes-Aldasoro
Hola
Primero te recomendaria escribir en ingles para aumentar la posibilidad de que respondan las preguntas. Segundo, para que quieres crear una nueva matriz de algo que ya tienes como una matriz? No es buena idea crear muchas variables, simplemente usa M(:,:,k) cada vez que necesites evaluar esos datos.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by