Deleting duplicate rows and keeping zero rows?

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! ;)

 채택된 답변

Stephen23
Stephen23 2016년 1월 28일
편집: Stephen23 2016년 1월 28일

1 개 추천

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

댓글 수: 1

Karmen
Karmen 2016년 1월 28일
Thank you 1000 times! I really appreciate your help!;) It works perfectly and the answer is exactly what i was asking for.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2016년 1월 28일

2 개 추천

Here is another way.
zeroRowIndex = find(~any(A,2)); % Rows with all zeros
[~,uniqueRowIndex] = unique(A,'rows'); % Unique rows (including all-zero rows)
rowsToKeep = union(uniqueRowIndex,zeroRowIndex); % Combine the criteria. [union will discard duplicates by default]
B = A(rowsToKeep,:); % Output

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2016년 1월 28일

댓글:

2016년 3월 1일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by