summation of answers received in a "for" loop

2 Ansichten (letzte 30 Tage)
R@SH
R@SH am 3 Sep. 2021
Kommentiert: R@SH am 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.

Akzeptierte Antwort

KSSV
KSSV am 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 Kommentare
KSSV
KSSV am 3 Sep. 2021
t1 and t2 are saved in array.
sum(t1+t2)
R@SH
R@SH am 3 Sep. 2021
I appreiate your favor

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by