How to get indexes of unique elements of a vector?
For instance if you have a vector v = [1,2,1,3,5,3]
, the unique elements are [1,2,3,5]
(output of unique
) and their indexes are ind = [1,2,4,5]
. What function allows me to compute ind
so that v[ind] = unique(v)
?
If you don't care about finding the first index for each unique element, then you can use a combination of the
unique
andindexin
functions:Which gets one index for each unique element of
v
inv
. These are all in base and works in 0.6. This is about 2.5 times slower than @Bogumil's function, but it's a simple alternative.This is a solution for Julia 0.7:
or similar working under Julia 0.6.3 and Julia 0.7:
and a shorter version (but it will not work under Julia 0.7):
It will probably not be the fastest.
If you need speed you can write something like (should work both under Julia 0.7 and 0.6.3):