MATLAB: Need some help with a tricky loop

loopmatrix manipulation

Hi, I'm looking to create a loop. Here's what I have:
SP_returns = 2778×1 array
for i = 2526:2777
q(i-2525) = quantile(SP_returns(i-2525:i),0.09);
end
Therefore q = 1×252 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.

Best Answer

Cell arrays can contain vectors of different length.
tail_SP{K} = SP_returns(SP_returns(K:2525+K) < q(1,K)) - q(1,K);