sum of array elements with specific condition?

조회 수: 2 (최근 30일)
MUKESH KUMAR
MUKESH KUMAR 2018년 8월 29일
댓글: MUKESH KUMAR 2018년 8월 29일
lets say I had A=[1 0 1 1 0 1 0 1 0 0 1 1 1 1]; then I want B(1)=sum of all A elements, B(2)= sum all elements of A from A(2) excluding a(1), similarly for B(3) = sum of all A elements with excluding A(1),A(2) and so on...then B should be B=[9 8 8 7 7 6 5 5 4 4 4 3 2 1] thanks

채택된 답변

Stephen23
Stephen23 2018년 8월 29일
>> fliplr(cumsum(fliplr(A)))
ans =
9 8 8 7 6 6 5 5 4 4 4 3 2 1

추가 답변 (1개)

jonas
jonas 2018년 8월 29일
편집: jonas 2018년 8월 29일
This seems to be what you are after:
fliplr(cumsum(fliplr(A)))
ans =
9 8 8 7 6 6 5 5 4 4 4 3 2 1
5th digit is off. Could it be a mistake on your end?

카테고리

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