Adding each row to previous row in a vector-Not Cumulative
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi , I've looked everywhere to find the solution for this but I couldn't find the answer. Here 's the problem. How to add each row in a vector with just the previous row? For example here is the process on this vector: A=[2 1 7 9 4] and the answer should be this vector: C=[3 8 16 13]
Thanks in advance.
댓글 수: 0
채택된 답변
추가 답변 (3개)
Jan
2011년 7월 20일
Easier and faster:
B = A(1:end-1) + A(2:end)
댓글 수: 2
Sukuchha
2011년 7월 21일
Jan, your method is better ! i feel like stupid now ! I need to remember always solve problems in a simplier way !
Jan
2011년 7월 21일
@Sukuchha: Often a direct translation of the human language is helpful. You've formulated your algorithm very clear already: "add each row in a vector with just the previous row". So you did solve the biggest part of the problem already. Most of the questions in this forum are much less clear.
Paulo Silva
2011년 7월 20일
C=arrayfun(@(x)plus(A(x),A(x-1)),2:numel(A))
or with a for loop
C=zeros(1,size(A,2)-1);
for x=2:size(A,2)
C(x-1)=A(x)+A(x-1);
end
C
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!