Delete rows from a matrix using for loop
39 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Juan Pérez Álvarez
am 22 Feb. 2022
Kommentiert: Dominique
am 6 Nov. 2023
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?
0 Kommentare
Akzeptierte Antwort
David Hill
am 22 Feb. 2022
Forcing to use for-loop
Matrix = [1,1 ; 1,2 ; 0,0 ; 0,0 ; 3,1 ; 0,0];
idx=[];
for k=1:size(Matrix,1)
if all(Matrix(k,:)==0)
idx=[idx,k];
end
end
Matrix(idx,:)=[];
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!