Dynamic variable names(ordered with subscript) in a loop creating new dynamic variables
3 views (last 30 days)
Show older comments
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 Comments
KSSV
on 8 Apr 2017
Dynamic labelling is a bad ides., Instead you define your vectors I to a matrix and access them.
Accepted Answer
Jan
on 8 Apr 2017
Edited: Jan
on 8 Apr 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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!