Asignar matriz automaticamente.

5 vues (au cours des 30 derniers jours)
David Carrillo
David Carrillo le 3 Mai 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 commentaire
Stephen23
Stephen23 le 3 Mai 2022
Modifié(e) : Stephen23 le 3 Mai 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:

Connectez-vous pour commenter.

Réponses (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.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by