Borrar filtros
Borrar filtros

Sum the component of a matrix till a certain value

4 visualizaciones (últimos 30 días)
luca
luca el 2 de Sept. de 2019
Comentada: luca el 2 de Sept. de 2019
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?

Respuesta aceptada

madhan ravi
madhan ravi el 2 de Sept. de 2019
Editada: madhan ravi el 2 de Sept. de 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 comentarios
madhan ravi
madhan ravi el 2 de Sept. de 2019
Done , see the edited answer.
luca
luca el 2 de Sept. de 2019
thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by