how do I remove rows with duplicated values?

5 ビュー (過去 30 日間)
jesus escareno
jesus escareno 2017 年 3 月 7 日
コメント済み: Otto Liljansalo 2024 年 3 月 14 日
let say i have matrix A
A = [1 2 3 4 5;
3 5 7 9 11;
1 1 4 5 7;
3 5 6 6 9;
1 4 10 15 16]
and wanted to remove all rows with values that repeated with each other. Then A will become
A = [1 2 3 4 5;
3 5 7 9 11;
1 4 10 15 16]
Thanks for the help in Advance

採用された回答

the cyclist
the cyclist 2017 年 3 月 7 日
編集済み: the cyclist 2017 年 3 月 7 日
Here is a one-liner:
A(any(diff(sort(A,2),[],2)==0,2),:)=[];
If you are certain that each row is already in numerical order (as in your example), you can skip the sorting step.
The algorithm is the following:
  • Use sort to sort the values in each row
  • Use diff to get the difference between consecutive values in each row
  • Use any to check if any of those differences equal zero (indicating a duplicate values)
  • Use the output of the any command, which is a logical index, to delete the rows that have duplicates
  3 件のコメント
Srinivas Kolluru
Srinivas Kolluru 2020 年 8 月 15 日
Works perfectly. Thank you
Otto Liljansalo
Otto Liljansalo 2024 年 3 月 14 日
Nice programming there!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by