Need some help with a tricky loop
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Walter Roberson
le 19 Juin 2011
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 commentaires
Walter Roberson
le 19 Juin 2011
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)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!