How to write a for loop?

16 vues (au cours des 30 derniers jours)
Karan Shah
Karan Shah le 27 Oct 2017
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);
  1 commentaire
Andrei Bobrov
Andrei Bobrov le 27 Oct 2017
READ about for..end.

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 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

Plus de réponses (2)

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

Bishwajit Roy
Bishwajit Roy le 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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by