how to Sum Array with "for" loop ?

조회 수: 13 (최근 30일)
Muhammad Pamungkas
Muhammad Pamungkas 2017년 5월 26일
댓글: Jan 2017년 5월 26일
need help, how i can get value of c(t)=b(1)+b(2)+...+b(t) in matlab ? e.g. a=0 for t=1:5 b=a+t end
will result b = [1] b = [1 2] b = [1 2 3] b = [1 2 3 4] b = [1 2 3 4 5]
then i want to make
c(1)=b(1) c(2)=b(1)+b(2) ... c(t)=b(1)+b(2)+...+b(t)
  댓글 수: 1
Jan
Jan 2017년 5월 26일
The question is not clear. "b=[1], b=[1,2]" is not a result, but multiple results assigned to the same variable.
Currently the best answer would be:
c = cumsum(1:5);
But is this really what you are looking for?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 5월 26일
편집: Stephen23 2017년 5월 26일
Use cumsum, e.g.:
>> B = 1:5;
>> cumsum(B)
ans =
1 3 6 10 15

추가 답변 (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