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.
2 件のコメント
KSSV
2017 年 4 月 8 日
Dynamic labelling is a bad ides., Instead you define your vectors I to a matrix and access them.
採用された回答
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 Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!