Asignar matriz automaticamente.

7 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2022-5-3
编辑:Stephen23 2022-5-3
"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:

请先登录,再进行评论。

回答(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.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by