how do I remove rows with duplicated values?

5 Ansichten (letzte 30 Tage)
jesus escareno
jesus escareno am 7 Mär. 2017
Kommentiert: Otto Liljansalo am 14 Mär. 2024
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

Akzeptierte Antwort

the cyclist
the cyclist am 7 Mär. 2017
Bearbeitet: the cyclist am 7 Mär. 2017
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 Kommentare
Srinivas Kolluru
Srinivas Kolluru am 15 Aug. 2020
Works perfectly. Thank you
Otto Liljansalo
Otto Liljansalo am 14 Mär. 2024
Nice programming there!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by