Sum the component of a matrix till a certain value

2 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(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