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

6 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 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

Più risposte (0)

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by