indexing in the nested loop

5 次查看(过去 30 天)
sum_y_2_s=0;
sum_x_2_s=0;
for s=0:1:T-1
sum_y_2_s(s)=sum_y_2_s;
sum_x_2_s(s)=sum_x_2_s;
for z=s:1:T-1
sum_y_2_s(s)=sum_y_2_s(s)+y.^(z-s);
sum_x_2_s(s)=sum_x_2_s(s)+x.^(z-s);
ratio_1=(sum_y_2_s(s)./(sum_x_2_s(s)+K^(1/gamma)*x.^(T-s)));
end
if ratio_1<1
s_asterisk=s(end);
return
end
end
How should I index loop correctly. It shows
Array indices must be positive integers or logical values.
Error in opt_ret_positive_wealth (line 18)
sum_y_2_s(s)=sum_y_2_s
I am trying to find this ratio.
Thank you very much

采纳的回答

Torsten
Torsten 2023-10-4
编辑:Torsten 2023-10-4
sx = 0;
sy = 0;
for s = T-1:-1:0
sx = sx + x^(T-1-s);
sy = sy + y^(T-1-s);
ratio(s+1) = sy/(sx+K^(1/gamma)*x^(T-s));
end
  3 个评论
Torsten
Torsten 2023-10-4
The answer has changed. If x or y equal 1, you get a division by zero from the old code. The code above should cover all possible cases for x,y > 0. You should compare the results from both codes.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by