0

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

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly :-). Thanks for helping out a rookie!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.