calculate the return between selected time points only
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
suppose I have a time series with stock prices and this variable is called "price" Now I have a second variable which is called "PSC1". This variable has n different elements which define the position of n stock prices in variable "price".
Now I want to calculate the simple returns between the selected stock prices, e.g. price(n)/price(n-1).
With my code below I always receive the famous error code:
"In an assignment A(I) = B, the number of elements in B and I must be the same."
Thank you for any advice!
%%%%%%%%%%%%%%%%%%%%%%%%%%%
for n = PSC1(1:end)
TF(n)=price(n+1)/price(n)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* So PSC1 has got twenty numbers, something like 52, 54, 102... Which are single positions in the variable "price" which has a length of 800 or smth. TF is supposed to be a variable which contains 20 returns
댓글 수: 0
채택된 답변
Orion
2014년 11월 24일
편집: Orion
2014년 11월 24일
Hi,
in your code, at the first iteration, you're trying to put a value in the 52th position of TF, which only have 20 elements => Problem !
you should do something like
for i = 1:length(PSC1)
TF(i) = price(PSC1(i)+1)/price(PSC1(i));
end
댓글 수: 2
Orion
2014년 11월 24일
Warning / advice
You always must predefine your arrays when you know the size they will have.
- your algorithm will be faster and more readable.
- this allows you to debug errors like the one you got.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Financial Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!