Sum the component of a vector till a certain value

조회 수: 7 (최근 30일)
luca
luca 2019년 8월 31일
답변: the cyclist 2019년 8월 31일
Hi,
given the vector G= [2 4 4 5 6 16 8 9 21 3 5 8 12 34 5 6 8] I want to obtain a new vector S with the same component but cutted at the element X when the sum of each single elements exceed a specific value Y.
So if in our case Y=80. the new vector S would be
S = [2 4 4 5 6 16 8 9 21 3], in this case the sum of the elements reach 78.
Adding the element 5 we obtain 83, exceeding our limit. That's why we cut at the element X=3, obtaining S= [2 4 4 5 6 16 8 9 21 3].
Does someone know hot to implement this?

채택된 답변

the cyclist
the cyclist 2019년 8월 31일
G(cumsum(G)<80)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by