Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find non-unique elements of an array in Matlab

Tags:

matlab

If I have an array [1 2 3 4 3 5 6 7 8 7], I'd like to find the list of non-unique entries: [3 7]. I fail to find a simple way to do it. Any idea?

Update: I'd like to have a universal solution, which would also work with cell array of strings.

like image 384
texnic Avatar asked Sep 05 '25 15:09

texnic


1 Answers

If A has length n, you can find the indices in A of the first occurrence of each entry and remove them from A:

A = [1 2 3 4 3 5 6 7 8 7];
n=length(A);
[~,IA,~] = unique(A);
out = unique(A(setdiff((1:n),IA)))
like image 50
Steve Avatar answered Sep 09 '25 01:09

Steve