MATLAB: Deleting duplicate rows and keeping zero rows

matrix arrayrows

Hi! I need a help!
A=[1 2;
3 4;
0 0;
1 2;
5 6;
0 0]
How can i delete duplicate rows, without deleting zero rows? I want my new matrix to look like that:
A=[1 2;
3 4;
0 0;
5 6;
0 0]
Thank you very much! 😉

Best Answer

A=[1 2;
3 4;
0 0;
1 2;
5 6;
0 0]
X = ~any(A,2)
[~,Y] = unique(A(~X,:),'rows','stable')
Z = X(~X)
Z(Y) = true
X(~X) = Z
B = A(X,:)
creates this:
B =
1 2
3 4
0 0
5 6
0 0