I have 21*1 Matrix. i have store it into z. then i have to slice these value as 1*1 Matrix and allocate (z1, z2, z3, z4.......z21) for 21 matrix. how can i allocate. please tell me command or allocation.
Mostrar comentarios más antiguos
z =
1.0e-03 *
1.0000
0.9000
0.8000
0.7000
0.6000
0.5000
0.4000
0.3000
0.2000
0.1000
0
-0.1000
-0.2000
-0.3000
-0.4000
-0.5000
-0.6000
-0.7000
-0.8000
0.1000
2 comentarios
Julia
el 8 de Jul. de 2015
Why do you need to allocate the values to 21 variables? You can access them with
z(i)
where i is between 1 and 21
Respuesta aceptada
Más respuestas (2)
Don't do this!
Please read:
- http://www.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop
- http://www.mathworks.com/matlabcentral/answers/56124-eval-is-evil-using-variables-created-dynamically-info-retrieval
Creating a list of variables dynamically and hiding an index in the name of the variable, is a really bad programming practice. Such complicated and indirect methods to create variables demand for even more complicated methods to use the variables later on. It will be nearly impossible to debug the code and the dynamic creation of variables slows down the processing substantially.
Follow Julia's comment and use z(i) instead.
Azzi Abdelmalek
el 8 de Jul. de 2015
I don't know why you need to create all these variables.
z=rand(1,21) % Example
for k=1:21
assignin('base',sprintf('z%d',k),z(k))
end
1 comentario
Stephen23
el 8 de Jul. de 2015
Please don't do this! Learn how to program properly in MATLAB, and it makes your own life easier. Read the other answers to know why.
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!