how do I change a variable name in each iteration?

조회 수: 15 (최근 30일)
Amy
Amy 2016년 2월 25일
댓글: Walter Roberson 2021년 4월 15일
I want to create a loop where the variable name changes with each iteration. Specifically, I want to do the following:
r_1 = .4
r_2 = .3
r_3 = .8
r_4 = .2
r_5 = .2
numArrays = 5;
a = cell(numArrays,1);
for n = 1:numArrays
A{n} = .5.*log((1+r_{n})./(1-r_{n}));
end
So in the last line the 'r_{n}' should be the variable 'r_1' for the first iteration, 'r_2' for the second, and so on.
I'm really stuck and I know this is a simple question, but any advice would be much appreciated!
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 4월 15일
@Mikkel Hansen comments to Stephne Cobeldick:
Not the same question nor same responses.

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

답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 25일
편집: Walter Roberson 2016년 2월 25일
Do not do that.
r_ = {r_1, r_2, r_3, r_4, r_5};
Now your cell array indexing will work.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by