Dynamic variable names(ordered with subscript) in a loop creating new dynamic variables
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Evrim Yilmaz
el 8 de Abr. de 2017
Comentada: Evrim Yilmaz
el 8 de Abr. de 2017
Hello everyone,
I am new here and quite new at programming too. I have been trying to write a code for my final research for 2 days. So couldn't find de right loop and asking for your precious help. I guess it may be quite easy for you though...
the equation is;
"P" s are vectors.
P_1 = [a b c]; P_2 = [d e f]; P_3 = [g h i]; P_4 = [j k l]; P_5 = [m n o];
letters are numbers like (0.5315, 1.0248, ...ets)
P_j = P_(i+1) - P_i and i = 1,..,5 ; j = 1,..,4
I can also name P_j as Pi+1,i (like P_j=1 = P_21 = P_2 - P_1).
Thanks for your answers.
2 comentarios
KSSV
el 8 de Abr. de 2017
Dynamic labelling is a bad ides., Instead you define your vectors I to a matrix and access them.
Respuesta aceptada
Jan
el 8 de Abr. de 2017
Editada: Jan
el 8 de Abr. de 2017
Don't do this.
Do not create variables dynamically and do not hide inidces in th names. Use numerical arrays instead:
P = [a b c; ...
d e f; ...
g h i; ...
j k l; ...
m n o];
Now P(i, :) is what you wanted to call "P_i". It is very easy to expand this until i = 1e6 and calculations with rows of matrices are trivial.
This is a very frequently asked question. Searching in the forum is a good strategy to start with. See e.g.: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!