MATLAB: Is there any method for implementing the UNIQUE function without sorting the array in MATLAB

increasinglistMATLABorderorderedre-arrangerearrangeremovesortunsort

When I execute the following code in MATLAB
x = [5 4 4 6 1];
unique(x)
I observe the following output
ans =
1 4 5 6
However, I would like to eliminate duplicate values without sorting values by ascending values.

Best Answer

This enhancement has been incorporated in Release 2012a (R2012a). For previous product releases, read below for any possible workarounds:
The ability to eliminate duplicates from an array without sorting is not available in MATLAB.
To work around this behavior, the indices returned by UNIQUE can be used to retain the order in the returned matrix.
x = [5 4 4 6 1];
[b,i,j]=unique(x, 'first')
b =
1 4 5 6
i =
5 3 1 4
j =
3 2 2 4 1
x(sort(i))
ans =
5 4 6 1