Pietro Z.
Pietro Z.

Reputation: 531

Remove rows containing duplicate elements

I have this example matrix:

1 2 4 5 1 3
2 3 5 6 3 4
1 2 3 4 5 6
3 2 4 6 1 5
...

I need to delete each row that contains duplicate elements. In this example I have to delete the first and second rows. I know how to do this in a for-loop, but I don't want to use a for-loop.

Upvotes: 1

Views: 82

Answers (1)

Divakar
Divakar

Reputation: 221564

Assuming A as the input matrix, you could do -

A(all(diff(sort(A,2),[],2),2),:)

Sample run -

>> A
A =
     1     2     4     5     1     3
     2     3     5     6     3     4
     1     2     3     4     5     6
     3     2     4     6     1     5
>> A(all(diff(sort(A,2),[],2),2),:)
ans =
     1     2     3     4     5     6
     3     2     4     6     1     5

Alternatively, if you don't mind some bsxFUN -

A(~any(sum(bsxfun(@eq,A,permute(A,[1 3 2])),2)>1,3),:)

Upvotes: 4

Related Questions