How can i make the sum of matrix elements>0 under the condition that there are min 3 zeros between the sums?

1 visualización (últimos 30 días)
I hava a matrix: a=[2 3 0 0 4; 0 0 0 0 0; 0 5 3 0 4; 14 4 0 1 1; 0 0 0 0 0; 0 0 0 7 9; 0 0 0 0 0; 0 0 0 0 0; 9 6 0 0 1; 1 1 1 0 0];
How can i make the sum of elements>3, under the condition that there are at least 3 zeros between the last element of one and the first element of the next sum?
Output should be: E=[9 32 16 19]
thanks!

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 23 de Abr. de 2016
Editada: Azzi Abdelmalek el 23 de Abr. de 2016
a=[2 3 0 0 4; 0 0 0 0 0; 0 5 3 0 4; 14 4 0 1 1; 0 0 0 0 0; 0 0 0 7 9; 0 0 0 0 0; 0 0 0 0 0; 9 6 0 0 1; 1 1 1 0 0]
b=reshape(a',1,[]);
id=~b;
ii=strfind(id,[1 1 1 0])+2;
ii=[0 ii numel(b)];
for k=1:numel(ii)-1
out(k)=sum(b(ii(k)+1:ii(k+1)));
end
out
  10 comentarios
hdiba
hdiba el 24 de Abr. de 2016
but the sums are not row sums. this was just an assumption you've done in the comment above.
hdiba
hdiba el 4 de Mayo de 2016
is there any way to get the number of elements in a sum? maybe somehow with the difference between the end position and beginning position of the sum? in our example for sum 9=2+3+0+0+4, there are 5 summands

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by