sum inside a for loob

1 次查看(过去 30 天)
tevzia
tevzia 2013-7-23
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 个评论
tevzia
tevzia 2013-7-23
I figured out
% %
a=0;
for i =1:5
a = a + (price1 * i)
% code
end

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by