Is there any method for implementing the UNIQUE function without sorting my array in MATLAB?
조회 수: 6 (최근 30일)
이전 댓글 표시
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.
채택된 답변
MathWorks Support Team
2012년 4월 10일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!