Dynamic variable names(ordered with subscript) in a loop creating new dynamic variables

3 次查看(过去 30 天)
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.

采纳的回答

Jan
Jan 2017-4-8
编辑:Jan 2017-4-8
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

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by