How to find first zero element per row in a matrix and set other elements to zero without loops
Ältere Kommentare anzeigen
Hi. I have a matrix of this form:
A = [ 1 2 3 0 4 5 0 0 1 1 1; 2 5 0 0 0 1 1 0 4 2 3; 4 5 2 3 0 1 2 3 4 5 6];
and i want for each line, starting from the first zero to get this type of matrix
B = [ 1 2 3 0 0 0 0 0 0 0 0; 2 5 0 0 0 0 0 0 0 0 0; 4 5 2 3 0 0 0 0 0 0 0];
I managed to get the index of each column where the first zero can be found, ie [4 3 5] but I cannot get the B matrix. Help will be greatly appreciated. Thanks!
Antworten (2)
Andrei Bobrov
am 27 Mär. 2015
Bearbeitet: Andrei Bobrov
am 27 Mär. 2015
B = A.*(cumsum(A==0,2)==0);
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!