How to write a for loop?

10 visualizaciones (últimos 30 días)
Karan Shah
Karan Shah el 27 de Oct. de 2017
Respondida: Bishwajit Roy el 28 de Oct. de 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);

Respuesta aceptada

KSSV
KSSV el 27 de Oct. de 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

Más respuestas (2)

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

Bishwajit Roy
Bishwajit Roy el 28 de Oct. de 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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by