Remove duplicate rows based on subset of row elements

Hello everyone,
I tried to find a similar problem - relatively simple I believe - but I could not find a solution. If, for instrance, I have a matrix such as
A = [1 1 1 100; 2 2 2 200; 3 3 3 300; 1 1 1 500]
how can I remove from A the fourth row based on the fact that only the thee first elements of the that row are identical to the three elmenets in the first row?
Also, what if I have
A = [1 1 1 100; 2 2 2 200; 3 3 3 300; 1 1 1 500; 1 1 1 600]
and, again, I just want to keep the first row?
Thank you for your help.

 採用された回答

madhan ravi
madhan ravi 2019 年 3 月 5 日
編集済み: madhan ravi 2019 年 3 月 5 日

0 投票

A(unique(A(1:3),'rows'),:)

5 件のコメント

madhan ravi
madhan ravi 2019 年 3 月 5 日
indices_to_be_removed = setdiff(1:size(A,1),unique(A(1:3),'rows'));
A(indices_to_be_removed,:) = []
Federico Maglione
Federico Maglione 2019 年 3 月 5 日
編集済み: Federico Maglione 2019 年 3 月 5 日
Thank you very much for your answer.
A(unique(A(1:3),'rows'),:)
works for the example I gave.
However it does not work if
A = [1 2 3 100; 4 5 6 200; 7 8 9 300; 1 2 3 500];
Could you still help me in this case (again want to remove the last row)?
Thank you
madhan ravi
madhan ravi 2019 年 3 月 5 日
Ah my bad typo:
[~,idx,~] = unique(A(:,1:3),'rows');
A(idx,:)
Federico Maglione
Federico Maglione 2019 年 3 月 5 日
Thank you! It works perfectly
Vijay
Vijay 2020 年 9 月 30 日
Simple, but an excellent solution.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by