1 view (last 30 days)
R@SH on 3 Sep 2021
Commented: R@SH on 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.

KSSV on 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
R@SH on 3 Sep 2021

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!