save variable with changing name

10 ビュー (過去 30 日間)
Amy
Amy 2016 年 2 月 25 日
編集済み: Stephen23 2019 年 6 月 25 日
Hello Matlabers,
I have a very easy question, but I can't seem to figure it out. I want to do the same calculation hundreds of times and save the output with a variable name that changes with each loop. Specifically, I want to do the following:
z_1 = .5.*log((1+r_1)./(1-r_1)); and then save as z_1
z_2 = .5.*log((1+r_2)./(1-r_2)); and then save as z_2
z_3 = .5.*log((1+r_3)./(1-r_3)); and then save as z_3
and so on...
any help would be much appreciated, thanks!
  3 件のコメント
Amy
Amy 2016 年 2 月 25 日
Okay great thank you. I will use a cell array instead. So I'm going to use something like the following:
numArrays = 676;
a = cell(numArrays,1);
for n = 1:numArrays
A{n} = .5.*log((1+r_1)./(1-r_1));
end
I'm still not sure how to change the variable from r_1 to r_2 to r_3, etc. in each loop. sorry for the naive question!
Steven Lord
Steven Lord 2016 年 2 月 25 日
Don't use r_1, r_2, etc. either.
numArrays = 677;
r = rand(1, numArrays); % Random data for demonstration purposes
z = 0.5*log((1+r)./(1-r));
This generates all the data at once; no loop, no variables named iteratively. Where in your code you would have used z_17 (for example) instead use z(17).

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by