remove duplicate rows from a matrix

I have a matrix of the form
A=[317.0000 282.0000 310.0000 259.0000 257.0000 305.0000 294.6667 282.0000
317.0000 282.0000 309.0000 372.0000 257.0000 305.0000 294.3333 319.6667
317.0000 282.0000 257.0000 305.0000 310.0000 259.0000 294.6667 282.0000
317.0000 282.0000 257.0000 305.0000 309.0000 372.0000 294.3333 319.6667
92.0000 166.0000 55.0000 235.0000 71.0000 173.0000 72.6667 191.3333
92.0000 166.0000 71.0000 173.0000 55.0000 235.0000 72.6667 191.3333];
I want to remove the redundant rows from A. Can anyone help. TIA.

 採用された回答

Thorsten
Thorsten 2016 年 8 月 16 日

0 投票

unique(sort(A,2), 'rows')

2 件のコメント

Ananya Malik
Ananya Malik 2016 年 8 月 16 日
Thnaks a lot @Thorsten. Works perfectly.
John Allen
John Allen 2020 年 8 月 14 日
warning, if position in the row is important then you dont want this. ie if (1,2) is different to (2,1)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 8 月 16 日

1 投票

uA = unique(A, 'rows', 'stable');

7 件のコメント

Ananya Malik
Ananya Malik 2016 年 8 月 16 日
編集済み: Ananya Malik 2016 年 8 月 16 日
Does not work. It returns back exactly A.
Guillaume
Guillaume 2016 年 8 月 16 日
"Does not work"
It does exactly what you asked. If it does not work that would be because no two rows are exactly identical. There may be some small differences between the values that matlab does not show by default.
You can use uniquetol instead of unique to make up for these small differences:
uA = uniquetol(A, 'ByRows', true); %use default tolerance. You can specify your own
There is no 'stable' option for uniquetol. The returned rows will be ordered.
Ananya Malik
Ananya Malik 2016 年 8 月 16 日
編集済み: Walter Roberson 2016 年 8 月 16 日
A simpler example
A = [1 2 3 45; 3 2 1 45; 1 4 5 54; 5 4 1 54];
I need the output
B= [1 2 3 45; 1 4 5 54]
Ananya Malik
Ananya Malik 2016 年 8 月 16 日
Thanks Guillaume for your input.
Walter Roberson
Walter Roberson 2016 年 8 月 16 日
You have a row that begins with 5, but no output row that begins with 5. You have removed too much.
John Allen
John Allen 2020 年 8 月 14 日
the post title is misleading - you don't want to remove duplicate rows, you want to remove rows with the same contents, irrespective of order
Walter Roberson
Walter Roberson 2020 年 8 月 14 日
[~, iA] = uniquetol(A, 'byrows', true);
A(sort(iA), :) %the unique rows, in the original order

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by