필터 지우기
필터 지우기

Creating an efficient for loop

조회 수: 1 (최근 30일)
Cary
Cary 2015년 8월 17일
댓글: Star Strider 2015년 8월 17일
for i = 1:length(startIdx)
for j = 1:length(date(startIdx(i):cutoffIdx(i)))
k = date(startIdx(i):cutoffIdx(i));
try
shortIdx(j)=find(and(and(and(and(and(jam>=1.2,jam<=2.3),expiration==xDates(i)),option_type=='c'),jamSym==1),quote_date==k(j)),1);
catch
warning('Not present')
shortIdx(j)=0;
end
end
end
Let's say i = 1:4. On the first pass through i, everything is fine. But when i = 2, I am overwriting the stored shortIdx data I created when i was 1. j is the length of each i, and k is the dates for each i. Where I get tripped up is the last part of the try statement
quote_date==k(j)
because when i turns to 2, j is reset to 1. So even though k is the correct set of dates for i, I am simply overwriting the shortIdx variable, instead of appending to it. Is there a way for me to solve this efficiently? Or do I need to create an independent for loop for each i? Thank you.

채택된 답변

Star Strider
Star Strider 2015년 8월 17일
I can’t run your code, but if ‘shortIdx’ is a single value (likely an index, considering the find call), one (probably the easiest) solution is to create a matrix out of ‘shortIdx’ so it increments with both ‘i’ and ‘j’:
shortIdx(i,j) = ...;
and then refer to it by both indices in your catch block.
  댓글 수: 2
Cary
Cary 2015년 8월 17일
Beautiful, Star. As always, thanks for your help!
Star Strider
Star Strider 2015년 8월 17일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by