i am trying to loop through three variables and store the data from each loop:
for rating=1:3
k=rating+1; %start rating
l=10; %end rating
AAADowngrade=zeros(100,l-k+1,rating);
for i=1:100
for j=k:l
AAADowngrade(i,j-k+1,rating) = sum(tHistorical(rating,j:l,i));
end
end
end
However, the code above only stores data from the final rating (rating =3). The first two tables just have zeros in them.

 採用された回答

Walter Roberson
Walter Roberson 2021 年 9 月 15 日
編集済み: Walter Roberson 2021 年 9 月 15 日

0 投票

maxrating = 3;
AAADowngrade = cell(maxrating,1);
l=10; %end rating
for rating = 1 : maxrating
k = rating+1; %start rating
thisAAADowngrade = zeros(100,l-k+1);
for i=1:100
for j=k:l
thisAAADowngrade(i,j-k+1) = sum(tHistorical(rating,j:l,i));
end
end
AAADowngrade{rating} = thisAAADowngrade;
end
You need a cell array because the arrays are not all the same size.

6 件のコメント

RP
RP 2021 年 9 月 15 日
Thanks. I tried pasting in this code and I got the following error:
Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in AST (line 41)
AAADowngrade{rating} = thisAAADowngrade;
Stephen23
Stephen23 2021 年 9 月 15 日
You need to include this line before the loop:
AAADowngrade = cell(maxrating);
Walter Roberson
Walter Roberson 2021 年 9 月 15 日
Corrected.
RP
RP 2021 年 9 月 15 日
I tried adding this line (not show if done correctly). The code runs but it only gives me results for the max rating (3). The other tables are blank.
maxrating = 3;
AAADowngrade = cell(maxrating);
l=10; %end rating
for rating = 1 : maxrating
k = rating+1; %start rating
AAADowngrade = zeros(100,l-k+1);
for i=1:100
for j=k:l
thisAAADowngrade(i,j-k+1) = sum(tHistorical(rating,j:l,i));
end
end
AAADowngrade = cell(maxrating);
AAADowngrade{rating} = thisAAADowngrade;
end
Walter Roberson
Walter Roberson 2021 年 9 月 15 日
no you need to use the version that I corrected
RP
RP 2021 年 9 月 15 日
Thanks - I have accepted your repsonse.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2019b

質問済み:

RP
2021 年 9 月 15 日

コメント済み:

RP
2021 年 9 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by