How to create a vector where the first element has a different formula to the rest of the vector?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have MXINret which is a 260-row vector with one stock index's returns for 260 consecutive trading days. I want to create a vector MXINcum which will find cumulative returns using MXINret. This is easy enough and will use the MATLAB-equivalent of the standard (1+x2)*(1+y1)-1 formula where x2 is the daily return for a certain day and y1 is the cumulative return for the previous day. However, I want the first element of my MXINcum matrix to simply copy the first element of the MXINret matrix. How do I do this? Using a if function (with/without a for loop) or is there a simpler way to do this?
Many thanks for your help ! This is my first question here so apologies if I've violated any forum customs !
댓글 수: 0
채택된 답변
Nirav Sharda
2017년 4월 19일
편집: Nirav Sharda
2017년 4월 19일
To set the first element of MXINcum to be the copy of the first element in MXINret, you can do.
MXINcum(1) = MXINret(1);
Once you have this you can do a for loop and set the other elements based on the formula. Also you can pre-allocate the vector MXINcum with zeros for speedup. I hope this helps.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!