How to write a for loop?

8 views (last 30 days)
Karan Shah
Karan Shah on 27 Oct 2017
Answered: Bishwajit Roy on 28 Oct 2020
I know the value for all the (10 values) udotreal, and I also know the value of uk for the first iteration. So How do I write a for loop for doing this:
uk1 = uk + udotreal(1,1);
uk2 = uk1 + udotreal(1,2);
uk3 = uk2 + udotreal(1,3);
uk4 = uk3 + udotreal(1,4);
uk5 = uk4 + udotreal(1,5);
uk6 = uk5 + udotreal(1,6);
uk7 = uk6 + udotreal(1,7);
uk8 = uk7 + udotreal(1,8);
uk9 = uk8 + udotreal(1,9);
uk10 = uk9 + udotreal(1,10);

Accepted Answer

KSSV
KSSV on 27 Oct 2017
ukn = zeros(1,10) ;
for i = 1:10
if i == 1
ukn(i) = uk+udotreal(i,i) ;
else
ukn(i) = ukn(i-1)+udotreal(i,i) ;
end
end

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 27 Oct 2017
Without loops:
ukk = cumsum([uk;udotreal(:)]);
ukk = ukk(2:end);

Bishwajit Roy
Bishwajit Roy on 28 Oct 2020
ukn = zeros(1,10) ;
for i = 1:10
if i == 1
ukn(i) = uk+udotreal(i,i) ;
else
ukn(i) = ukn(i-1)+udotreal(i,i) ;
end
end

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!