How to write a loop for adding number

Hi,
I have 52 values a,b,c........
I need to do like below
b=a+b; c=b+c; d=c+d;............for all 52 values
How can I do this by writing a loop?
Thanks in advance

Respuestas (2)

Geoff
Geoff el 15 de Mzo. de 2012
If you think about what you're doing, it's just a cumulative sum.
Put all your variables into a vector:
vec = [a b c d .... ];
And use the cumulative sum.
out = cumsum(vec);
No loop necessary.
Jan
Jan el 15 de Mzo. de 2012

0 votos

Creating 52 different variables is less efficient than creating a [1 x 52] vector. Then the cumsum approach will be easy.

Categorías

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

Productos

Etiquetas

Preguntada:

Jim
el 15 de Mzo. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by