Reputation: 549
I am trying to calculate the residuals (res) for my 19 time series variables. I expect a (nIndices,:) matrix as an outcome from the calculation. The res calculation works perfectly if applied to 1 individual time series, however it does not work if I try to calculate it with a loop for all time series. It will only calculate res for the first time series (thus I get a 1,: matrix instead of nIndices,:). (The nIndices function correctly calculated 19 time series and was applied in the same editor file many times before).
I would appreciate any hints on what I am missing here.
for i=1:nIndices
res = returns(:,i)-mean(returns(:,i));
end
Upvotes: 0
Views: 46
Reputation: 1881
You forgot to iterate over the res variable to store the results. The way it is, it is overwriting the values assigned after each iteration. You should try:
for i=1:nIndices
res(:,i)=returns(:,i)-mean(returns(:,i));
end
You could also vectorize your approach by using the bsxfun:
res = bsxfun(@minus,returns,mean(returns,1));
Upvotes: 1