Adding the elements to next element

조회 수: 5 (최근 30일)
Offroad Jeep
Offroad Jeep 2015년 9월 21일
편집: James Tursa 2015년 9월 21일
Hi to all, my question is as follows
initial_A = 0 Let A = [1 2 3 4 5 6]
I want to add initial_A to A(1) the the sum will be 1 , next add 1 to 2 and sum will be 3, next add 3 to 3 and sum will be 6 , now add 6 to 4 sum will be 10, now 10+5 =15, and lastly 15 + 6 = 21. In the end the resultant matrix should be [1 3 6 10 15 21]
Regards....

채택된 답변

James Tursa
James Tursa 2015년 9월 21일
편집: James Tursa 2015년 9월 21일
result = cumsum([initial_A A]);
  댓글 수: 1
James Tursa
James Tursa 2015년 9월 21일
편집: James Tursa 2015년 9월 21일
Note: The answer shown corresponds to the original post which requested an output of [0 1 3 6 10 15 21]. To get simply [1 3 6 10 15 21] starting with A_initial, one could do this:
result = initial_A + cumsum(A);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by