Need some help with a tricky loop

조회 수: 7 (최근 30일)
Julia
Julia 2011년 6월 19일
Hi, I'm looking to create a loop. Here's what I have:
SP_returns = 2778x1 array
for i = 2526:2777
q(i-2525) = quantile(SP_returns(i-2525:i),0.09);
end
Therefore q = 1x252 array
Now I need to express the following as a loop:
tail_SP1 = SP_returns(SP_returns(1:2526) < q(1,1)) - q(1,1);
tail_SP2 = SP_returns(SP_returns(2:2527) < q(1,2)) - q(1,2);
tail_SP3 = SP_returns(SP_returns(3:2528) < q(1,3)) - q(1,3);
...
tail_SP252 = SP_returns(SP_returns(252:2777) < q(1,252)) - 1(1,252);
Basically, tail_SP1 will be in column 1, tail_SP2 will be in column 2 in the array if I do the loop. Problem is, they have different number of observations. For example, tail_SP1 and tail_SP2 may have 227, while tail_SP10 may have 226. The loop won't work. At least not the one I know how to do. I don't think it's even possible to create a matrix which would have different number of values in each column. Perhaps there is another way of doing this thing. I really don't want to have to type 252 lines of code for this. I will appreciate all the help I can get. Thanks.

채택된 답변

Walter Roberson
Walter Roberson 2011년 6월 19일
Cell arrays can contain vectors of different length.
tail_SP{K} = SP_returns(SP_returns(K:2525+K) < q(1,K)) - q(1,K);
  댓글 수: 10
Julia
Julia 2011년 6월 19일
Never mind, I got it.
Walter Roberson
Walter Roberson 2011년 6월 19일
Sorry, it was my turn to cut the front lawn ;-)
Just for completeness, in case someone else reads this and wonders what the solution is, the syntax would be
paramEsts{5}(1)

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

추가 답변 (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