3

I have an array a as follows:

a = [ 1 2; 3 4; 1 2 ];

I want to delete all rows appearing more than once in a and get c:

c = [ 3 4 ];

Please note that this is not the same operation as keeping unique rows, since I don't want rows that had duplicates to appear at all. How can I accomplish this?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
fili
  • 59
  • 5
  • Possible duplicate of [Eliminate/Remove duplicates from array Matlab](https://stackoverflow.com/questions/40393513/eliminate-remove-duplicates-from-array-matlab) – rahnema1 Feb 06 '18 at 16:34

1 Answers1

9

The third output of unique gives you the index of the unique row in the original array. You can use this with accumarray to count the number of occurrences, which can be used to select rows that occur only once.

For example:

A = [1 2; 3 4; 1 2];

[uniquerow, ~, rowidx] = unique(A, 'rows'); 
noccurrences = accumarray(rowidx, 1);

C = uniquerow(noccurrences==1, :);

Which returns:

>> C

C =

     3     4
sco1
  • 12,154
  • 5
  • 26
  • 48