How to find first zero element per row in a matrix and set other elements to zero without loops
이전 댓글 표시
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!
답변 (2개)
Andrei Bobrov
2015년 3월 27일
편집: Andrei Bobrov
2015년 3월 27일
B = A.*(cumsum(A==0,2)==0);
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!