Is there any method for implementing the UNIQUE function without sorting my array in MATLAB?
20 views (last 30 days)
Show older comments
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.
Accepted Answer
MathWorks Support Team
on 10 Apr 2012
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
0 Comments
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!