필터 지우기
필터 지우기

Sum the component of a matrix till a certain value

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

채택된 답변

madhan ravi
madhan ravi 2019년 9월 2일
편집: madhan ravi 2019년 9월 2일
ix = cumsum(M(:))<80;
v = mod(nnz(ix),size(M,1));
[~,c] = ind2sub(size(M),nnz(ix));
Wanted = M(:,1:c-(v~=0))
  댓글 수: 4
madhan ravi
madhan ravi 2019년 9월 2일
Done , see the edited answer.
luca
luca 2019년 9월 2일
thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by