필터 지우기
필터 지우기

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
KSSV 2017년 4월 8일
Dynamic labelling is a bad ides., Instead you define your vectors I to a matrix and access them.
Jan
Jan 2017년 4월 8일
This is not twitter: No # before the tags, please.

댓글을 달려면 로그인하십시오.

채택된 답변

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