フィルターのクリア

Nested for loop troubles

1 回表示 (過去 30 日間)
Cary
Cary 2015 年 6 月 29 日
コメント済み: Walter Roberson 2015 年 6 月 30 日
Hi Everyone,
I am having difficulty troubleshooting a nested for loop. Things work fine on the first pass (when i=49) but break down on the second pass (when i=50). More specifically, instead of appending to consolFut1 on the second pass, it overwrites the data from the first pass. Can anyone spot the error? Thanks for reading.
for i = 49:52
exp=find(tifDate==expDate(i));
for j = [1; 18; 43; 63]
tifCls1 = tifCls(j:exp);
for k = [1112; 1129; 1154; 1174]
consolFut1=consolFut(k:expDateIdx(i),i);
arbTIF=tifCls1-consolFut1;
break;
end
end
end
  2 件のコメント
James Tursa
James Tursa 2015 年 6 月 29 日
編集済み: James Tursa 2015 年 6 月 29 日
What are the dimensions of your variables? The usual procedure is to pre-allocate an array to contain the outputs from each iteration. I assume the break statement is there for debugging, because as written it will always exit the k loop on the first iteration.
Cary
Cary 2015 年 6 月 30 日
I would be really grateful to anyone who can help me solve this issue. I've spent all day and night trying to fix it. On the second pass, consolFut1 overwrites the column instead of adding to the bottom of it. I don't understand why it does this as tifCls1 works perfectly. Thank you for your time. I sincerely appreciate it.

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

回答 (1 件)

Image Analyst
Image Analyst 2015 年 6 月 29 日
Nothing on the left hand side of your equations takes an index, so they all get overwritten at each iteration. Add an index if you want to store the different values.
exp is a built in function. Don't use it as the name of a variable!
  4 件のコメント
Cary
Cary 2015 年 6 月 29 日
I'm not trying to append to the right. On the first pass the data is stored in consolFut1 (a column). On the second pass, instead of appending to the bottom of the column, it overwrites the data from the beginning. The column should be getting longer and longer with each pass, while keeping the old data.
Walter Roberson
Walter Roberson 2015 年 6 月 30 日
consolFut1 = [ consolFut1; consolFut(k:expDateIdx(i), i).' ];
Having multiple elements in the first dimension of the consolFut is for selecting part of a row. If you want those elements to be written into a column then you need to transpose the row to become a column.

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

カテゴリ

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