Sum the component of a matrix till a certain value

1 view (last 30 days)
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?

Accepted Answer

madhan ravi
madhan ravi on 2 Sep 2019
Edited: madhan ravi on 2 Sep 2019
ix = cumsum(M(:))<80;
v = mod(nnz(ix),size(M,1));
[~,c] = ind2sub(size(M),nnz(ix));
Wanted = M(:,1:c-(v~=0))
  4 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!