How to write a loop for adding number
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Geoff
2012-3-15
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.
0 个评论
Jan
2012-3-15
Creating 52 different variables is less efficient than creating a [1 x 52] vector. Then the cumsum approach will be easy.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!