How to create a vector where the first element has a different formula to the rest of the vector?

3 次查看(过去 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 !

采纳的回答

Nirav Sharda
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by