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 !

採用された回答

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by