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

2 vues (au cours des 30 derniers jours)
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 !

Réponse acceptée

Nirav Sharda
Nirav Sharda le 19 Avr 2017
Modifié(e) : Nirav Sharda le 19 Avr 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by