sum inside a for loob

hi everyone, I was wondering how to sum my calculation inside loop. here is a simple code:
%
for i=1:5
a = price1 * i;
b =sum(a)
%
so how to sum a, when i type like this it gives result (b=a) for every times. It does not add(sum) all of the results

1 comentario

tevzia
tevzia el 23 de Jul. de 2013
I figured out
% %
a=0;
for i =1:5
a = a + (price1 * i)
% code
end

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 23 de Jul. de 2013

0 votos

b = 0;
for k = 1:5
a = price1 * k;
b = b + sum(a); % Or b = b + a?
end

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 23 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by