adding vector entry after each element

This is a very basic question but how would I add the elements of a vector like so...:
x = [1,2,3,4,5,6];
ans = 3 6 10 15 21

 채택된 답변

Matt Fig
Matt Fig 2012년 8월 15일
편집: Matt Fig 2012년 8월 15일

0 개 추천

x = [1,2,3,4,5,6];
y = cumsum(x)
y = y(2:end) % To match your example....

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 8월 15일

0 개 추천

And just for fun:
filter(x(2:end),1,ones(1,numel(x)-1))+x(1)

댓글 수: 1

Matt Fig
Matt Fig 2012년 8월 15일
편집: Matt Fig 2012년 8월 15일
While we're on the topic of ridiculously inefficient alternatives to CUMSUM, ;-)
sum(triu(toeplitz(x(2:end))))+x(1)

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

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

태그

질문:

2012년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by