summation of answers received in a "for" loop

2 vues (au cours des 30 derniers jours)
R@SH
R@SH le 3 Sep 2021
Commenté : R@SH le 3 Sep 2021
I want to collect the output of a for loop in both summation of the answers and the answers received in each stage of this process. In specific case, I want to have summation of the 5 answers obtained from below code. here "t_layer(i)" give me the answer obtained in each stage. I wanted to have summatuon of them in t_totall. But the code which I have written only gives me the last loop as t_total.
t_liner=1.5
for i=1:5
if rem(i,2)==0
t1(i)=i+3;
t2(i)=0;
elseif rem(i,2)==1
t2(i)=i-1;
t1(i)=0;
end
t_layer(i)=t1(i)+t2(i);
t_total=t_liner+t1(i)+t2(i);
Hope to have your instruction on this matter.

Réponse acceptée

KSSV
KSSV le 3 Sep 2021
t_liner=1.5 ;
t1 = zeros(5,1) ;
t2 = zeros(5,1) ;
t_layer = zeros(5,1) ;
t_total = zeros(5,1) ;
for i=1:5
if rem(i,2)==0
t1(i)=i+3;
t2(i)=0;
elseif rem(i,2)==1
t2(i)=i-1;
t1(i)=0;
end
t_layer(i)=t1(i)+t2(i);
t_total(i)=t_liner+t1(i)+t2(i);
end
  3 commentaires
KSSV
KSSV le 3 Sep 2021
t1 and t2 are saved in array.
sum(t1+t2)
R@SH
R@SH le 3 Sep 2021
I appreiate your favor

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by