Why is this function returning the wrong size matrix

1 view (last 30 days)
I wrote a very simple function to calculate stock price return regardless of the number of companies listed in the array. The company stock prices are listed in separate columns. The function code is:
function percentReturn=retCalc(prices)
percentReturn=prices(1:end-1,:)./prices(2:end,:)-1;
I would assume that this function should calculate the return regardless of the number of columns in the matrix. However, it just returns the first column of return. Can someone please tell me what I'm doing wrong?
  1 Comment
Cedric
Cedric on 30 Mar 2013
Edited: Cedric on 30 Mar 2013
Did you check that the local variable prices has really the correct size? If you add the line
size(prices)
right before the line computing percentReturn, you might see that prices doesn't have as many columns (companies) as what you think. Well, reading again your statement .. the way you perform your computation suggests that there is one company per column, and therefore that prices vary along rows, which is not what you seem to indicate above.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!