Delete rows from a matrix using for loop
이전 댓글 표시
Hello, I need delete the zero rows from a matrix using for loops/while, I try this:
Matrix = [1,1 ; 1,2 ; 0,0 ; 0,0 ; 3,1 ; 0,0];
LM =length(Matrix);
cont = 0;
for i = 1 : LM
if Matrix(i) == 0
cont = cont + 1;
end
end
Matrix_Aux = [];
for j = 1:LM
if Matrix(j) ~= 0
Matrix_Aux = [Matrix(j)];
end
end
I need get this:
Matrix_Aux = [1,1 ; 1,2 ; 3,1 ];
Any idea?
채택된 답변
추가 답변 (1개)
No loops are necessary:
Matrix = [1 1; 1 2; 0 0; 0 0; 3 1; 0 0]
Matrix(all(Matrix == 0,2),:) = []
카테고리
도움말 센터 및 File Exchange에서 Matrix Computations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!