Effacer les filtres
Effacer les filtres

indexing in the nested loop

5 vues (au cours des 30 derniers jours)
Raushan
Raushan le 4 Oct 2023
Commenté : Raushan le 4 Oct 2023
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

Réponse acceptée

Torsten
Torsten le 4 Oct 2023
Modifié(e) : Torsten le 4 Oct 2023
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 commentaires
Torsten
Torsten le 4 Oct 2023
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.
Raushan
Raushan le 4 Oct 2023
I got you, thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by