Cumulative sum for a vector

조회 수: 3 (최근 30일)
Teemu
Teemu 2013년 3월 2일
I have vector 1x5 (1,5,3,2,8) and I like to do cumulative sum of that. So the answer should seems like (6,9,11,19). Can you help me how to do it without loop?

채택된 답변

Wayne King
Wayne King 2013년 3월 2일
편집: Wayne King 2013년 3월 2일
x = [1,5,3,2,8];
cumsum(x)
but the first element is 1, not 6 as you have indicated. Of course, if you don't want that first element, you can do:
x = [1,5,3,2,8];
y = cumsum(x);
y = y(2:end);
  댓글 수: 1
Teemu
Teemu 2013년 3월 2일
Thanks, it was simple solution!

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

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by