summation of answers received in a "for" loop

2 visualizaciones (últimos 30 días)
R@SH
R@SH el 3 de Sept. de 2021
Comentada: R@SH el 3 de Sept. de 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.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by