For loop in a for loop

6 ビュー (過去 30 日間)
Jack Tse
Jack Tse 2021 年 3 月 26 日
コメント済み: Jack Tse 2021 年 3 月 27 日
I am writing a for loop with two data set A and B as the following:
for k = 1:5
for i = 12*k
b = nan(5,10)
for j = 1:10
a = regress(A(i-11:i,j),B(i-11:i,1))
b(k,j) = a(2:end)
end
end
end
I want to store all the result in one data set. However, only data with k = 5, i =5 is store into b. Any idea why this might happen?
  1 件のコメント
madhan ravi
madhan ravi 2021 年 3 月 26 日
i is a scalar

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

採用された回答

Rik
Rik 2021 年 3 月 26 日
You are resetting b in your loop, so only the last value of k matters.
And the middle loop isn't a loop, as it is a scalar.
  3 件のコメント
Rik
Rik 2021 年 3 月 27 日
By putting the code that assigns a value to b (without taking into account any existing value of b) outside the loop.
b = nan(5,10);
for k = 1:5
i = 12*k;
for j = 1:10
a = regress(A(i-11:i,j),B(i-11:i,1));
b(k,j) = a(2:end);
end
end
disp(b)
Jack Tse
Jack Tse 2021 年 3 月 27 日
Thank you so much. It work prefectly fine.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by