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

1 件のコメント

Karmen
Karmen 2016 年 3 月 1 日
Works great also! Thank you!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

質問済み:

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