How to create a vector where the first element has a different formula to the rest of the vector?
2 views (last 30 days)
Show older comments
Siddhant Shivram
on 14 Apr 2017
Edited: Nirav Sharda
on 19 Apr 2017
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 Comments
Accepted Answer
Nirav Sharda
on 19 Apr 2017
Edited: Nirav Sharda
on 19 Apr 2017
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 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!