필터 지우기
필터 지우기

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