Dear all,
I am trying to get an index for the variable y that starts at 500 in t=0 and is carried forward from period t=1 onwards by multiplying the variable y with ins_urz. In other words, I am trying to set a starting value for the variable y of 500 in t=0, and then multiply it by ins_urz from period t=1 onwards, in order to use the result of the multiplication in the respective period to multiply it by the continuous elements of ins_urz.
for t=0:249
y(1,1)=500;
y(t+1,:)=y(t,1).*ins_urz(t+1,:);
end
Unfortunately, this has not worked so far, because I get the error message "Index in position 1 is invalid. Array indices must be positive integers or logical values" every time I try.
Can someone help me how to insert the index correctly into the codes?
Thanks a lot for your help!

 採用された回答

Torsten
Torsten 2023 年 2 月 21 日

1 投票

y = cumprod([500;ins_urz])

4 件のコメント

Jan
Jan 2023 年 2 月 21 日
It sounds like ins_urz is a matrix.
Amelie
Amelie 2023 年 2 月 21 日
ins_urz is a (250,1) vector
Torsten
Torsten 2023 年 2 月 21 日
ins_urz is a (250,1) vector
Then the line of code will do what you want.
Amelie
Amelie 2023 年 2 月 21 日
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2023 年 2 月 21 日
編集済み: Jan 2023 年 2 月 21 日

1 投票

y(1, 1) = 500; % Set initial value before the loop
for t = 1:249 % y(1, :) is defined already, so start at t=1, not t=0
y(t+1, :) = y(t, 1) * ins_urz(t+1, :);
end

1 件のコメント

Amelie
Amelie 2023 年 2 月 21 日
Now it works!
Thank you very much, that was very helpful!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2023 年 2 月 21 日

コメント済み:

2023 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by