cut of zeros from a matrix based on the longest non-zero row
Mostrar comentarios más antiguos
Hi all,
I want to cut all zeros from the right, based on "the longest non zero row". an example:
A=[ 1 2 0 0 0 0;3 4 5 0 0 0;5 6 0 0 0 0];
output: A=[ 1 2 0 ;3 4 5 ;5 6 0 ];
thanks in advance!
1 comentario
Yongjian Feng
el 22 de Nov. de 2021
Try it yourself first please. We can then look at the code together.
Respuesta aceptada
Más respuestas (1)
Hi
your code line can look like this:
% a = some matrix
idx = max(sum(cumprod(a~=0,2)),2)
output = a(:,1:idx)
I suggest you break the one line into several ones just to see what each function does.
hope this helps.
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!