how do I change a variable name in each iteration?
18 ビュー (過去 30 日間)
古いコメントを表示
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
2021 年 4 月 15 日
@Mikkel Hansen comments to Stephne Cobeldick:
Not the same question nor same responses.
Walter Roberson
2021 年 4 月 15 日
It is the same question. See https://www.mathworks.com/matlabcentral/answers/270069-save-variable-with-changing-name#comment_345592
回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Argument Definitions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!