Finding the first zero element in a row and fill the entire row with the last non zero element
조회 수: 1 (최근 30일)
이전 댓글 표시
I have an m*n matrix which has only zeros and natural numbers. If there is a zero in a row, all the remaining elements after the first zero are also zeros. I would like to replace all zero elements with the last non zero element in the row. I am looking for the fastest way to do it in Matlab. Is there anyway without a loop? if not, what is the best way? Thanks.
For example:
A = [1 3 1 0 0 0 0;
1 4 2 1 3 4 1;
2 3 1 2 3 3 0];
% and I would like to have
B = [1 3 1 1 1 1 1;
1 4 2 1 3 4 1;
2 3 1 2 3 3 3];
댓글 수: 0
채택된 답변
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!