views:

357

answers:

1

Is there a Mathematica function that provides results equivalent to the unique() function in MATLAB? I realize I could use the Union[] function to obtain the unique elements of a list, but I would like something equivalent to the three-result version of the function that provides index arrays that map between the input array and the array of unique values.

If there is nothing built in, is there an implementation of that function available somewhere? Does someone here know how to build it?

+5  A: 

You can easily build similar functionality yourself with Mathematica's Position[]. E.g. given a list of numbers you could do the following:

In[1] := A = {1, 5, 2, 3, 7, 3, 2, 8, 6, 5, 9, 2, 1};
In[2] := {#, Flatten[Position[A, #]]} & /@ Union[A]
Out[2]:= {{1, {1, 13}}, {2, {3, 7, 12}}, {3, {4, 6}}, {5, {2, 10}}, {6, {9}}, {7, {5}}, {8, {8}}, {9, {11}}}

to get the list of unique elements and the indices of where they appear in the original list. To replicate exactly the functionality of Matlab's Unique(), especially for

[b,m,n] = unique(A)

you need

b = Union[A];
m = Last[Position[A, #]] & /@ b // Flatten;
n = Position[b, #] & /@ A // Flatten;

which now provide the desired behavior

In[1] := A[[#]] & /@ m == b
Out[1]:= True

In[2] := b[[#]] & /@ n == A
Out[2]:= True
Timo
That's exactly what I was looking for. I had forgotten all about Position[] and didn't find it again when browsing the documentation. Thanks for the help!
clartaq
One thing to consider is that `Union` will sort the input list. If you don't want that to happen, use `DeleteDuplicates`. `DeleteDuplicates` also tends to be substantially faster for large inputs.
Pillsy
Thanks for the tip, I'll have to do some testing, but that'll probably be a good improvement in a project of mine.
Timo

related questions