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 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