How to write a for loop?
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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);
Risposta accettata
KSSV
il 27 Ott 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
0 Commenti
Più risposte (2)
Andrei Bobrov
il 27 Ott 2017
Without loops:
ukk = cumsum([uk;udotreal(:)]);
ukk = ukk(2:end);
0 Commenti
Bishwajit Roy
il 28 Ott 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
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!