Creating one big loop from smaller ones

Hi, I'm trying to come up with a loop for the following:
SP = 2778x1 array
w = 1x252 array
alpha = 1x252 array
beta = 1x252 array
ss = zeros(2527,252);
ss(1,:) = 0;
I need to create one loop which would produce a 2527x252 array for the results for the following 252 smaller loops:
for i = 1:2526
ss(i+1) = w(1,1) + alpha(1,1).*SP(i) + beta(1,1).*ss(i);
end
for i = 2:2527
ss(i+1) = w(1,2) + alpha(1,2).*SP(i) + beta(1,2).*ss(i);
end
..........
for i = 252:2577
ss(i+1) = w(1,252) + alpha(1,252).*SP(i) + beta(1,252).*ss(i);
end
The code I've used is as follows:
for j = 1:252
for i = j:2526+j-1
ss(i+1,j) = w(1,j) + alpha(1,j).*SP(i) + beta(1,j).*ss(i);
end
This code works but it doesn't present the results the way I want i.e. it produces not 2527x252 array but a 2778x252 array and it looks like this:
1) 0 0 0 ... 0
2) # 0 0 ... 0
3) # # 0 ... 0
4) # # # ... 0
... ... ... ... ... ...
2527) # # # ... #
2528) 0 # # ... #
2529) 0 0 # ... #
... ... ... ... ... ...
2778) 0 0 0 ... #
What I need is for this array to have only one initial zero on top of every column. First column was perfect up to observation 2527. I need column two, three and so on to be pulled up, so that each has one zero on top and then another 2526 observations (that makes it 2527 in total). I would appreciate any help I can get on this one.

 Réponse acceptée

Jan
Jan le 19 Juin 2011
Before I give an advice about a much faster vectorized approach - does this create the wanted output:
EDITED: Index of ss adjusted to Julia's comments:
for j = 1:252
for i = j:2526+j-1
ss(i-j+2, j) = w(j) + alpha(j) * SP(i) + beta(j) * ss(i-j+1, j);
end
end
This takes 0.06 sec on my old 1.5GHz Pentium-M. A vectorization would be useful only if you run this funciton thousands of times. Do you?

13 commentaires

Andrew Newell
Andrew Newell le 19 Juin 2011
I think that ss(i) should be replaced by ss(i,j).
Julia
Julia le 19 Juin 2011
Thanks Jan, this is exactly what I was looking for.
Andrew Newell
Andrew Newell le 19 Juin 2011
Hey, wait, he was going to vectorize it for you!
Julia
Julia le 19 Juin 2011
I know that, that's why I'm checking this page every 15 mins or so.
Jan
Jan le 19 Juin 2011
And is it really ss(i), which is equivalent to ss(i,1), or do you want ss(i,j) as Andrew guessed?
Julia
Julia le 19 Juin 2011
ss(i) was fine. I tried ss(i,j) but it didn't work even though it seems like it should be more appropriate.
Julia
Julia le 19 Juin 2011
Actually, now that I think about it, it's not i, it's going to be (i-j+2-1) = (i-j+1). It's the previous observation.
Julia
Julia le 19 Juin 2011
Basically, if we're looking to find ss(5,6) for example, then we use the previous observation in the same column i.e. ss(4,6).
Julia
Julia le 19 Juin 2011
I made an adjustment. I used ss(i-j+1,j) and it worked and it also gave me values which I would expect based on theory and data.
Jan
Jan le 19 Juin 2011
@Julia: It seems to me, like you perform a stochastic index permutation. See: http://en.wikipedia.org/wiki/Programming_by_permutation
Therefore I will wait some time with further improvements of my code until your indices reach a stable level ;-)
Julia
Julia le 19 Juin 2011
This programming by permutation is exactly what I was doing. I'm not a programmer, I have started using MATLAB on and off about 3 months ago. I'm hopeless with code. But I do know theory behind what I'm doing (well, at least I think I do) and when I ran the code the first time I got results which seemed ok at first, but then I used those results to compute something else and I was surprised at what I got. It didn't seem right. When I re-run the code as per my adjustment, I got exactly what I needed. So my indices have reached a stable level :).
By the way, I'm going to post another loop question in about 5 minutes. This one is going to be more tricky. I don't even know if it's possible to do what I want to do with a loop.
Jan
Jan le 19 Juin 2011
@Julia: With the joined power of Matlab and Walter anything is possible - if it is at least deterministic and can be defined by a primitive recursive function.
Julia
Julia le 19 Juin 2011
@ Jan: I don't run it thousands of times. I have 3GHz Pentium 4 as far as I can remeber, so the speed would improve. But really, even 0.06 sec is nothing. I was using garchfit function to estimate beta, alpha and w above and it took me about 2 mins 40 secs to do it (I had enough time to go downstairs and make myself some tea :) ), so 0.06 sec is nothing.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by