why can't I use unique with 'stable' and 'last'?
조회 수: 9 (최근 30일)
이전 댓글 표시
The default behavior when using unique() with the 'stable' option is to return the indices to the first occurrence of each unique value. In case I want the last occurrence I would expect to use 'last' in conjunction with 'stable' option but the function doesn't allow me to use the two, it's either 'stable', or 'last'. Was the function designed to behave like this, or am I misunderstanding the usage?
This is what I'm trying to do
[C,ia,ic] = unique(A,'stable','last')
댓글 수: 0
채택된 답변
Matt J
2012년 12월 13일
편집: Matt J
2012년 12월 14일
Seems like a worthwhile enhancement request to me. However, currently (R2012a), the documentation for UNIQUE doesn't list
unique(A,'stable','last')
as a possible syntax, so it stands to reason that it's not currently allowed.
댓글 수: 1
QIAO WANG
2019년 4월 30일
Unfortuanely, though it's 2019 now (I'm using R2018a), this is still not allowed. That's a shame anyway. When I started using unique function recently, I thought unique(A,'stable','last') should be something natural to do. But I was wrong. Luckily, I got duration values so I could just sort again, kinda of workaround for me. However, if this syntax could be allowed in the future, it will definitely benefit more people like me.
추가 답변 (1개)
Image Analyst
2012년 12월 14일
Can't you just use fliplr() to reverse your vector, so now the last becomes the first? Then subtract the indexes from the length of the vector and add 1. So if you had a vector of length 10 and it had, say, 3 5's in it with the last one being at index 9, flipping it puts that at index 2, which is now the first one it will find. So 10-2+1 = 9, the original index.
댓글 수: 1
Matt J
2012년 12월 14일
Yes, that would be the thing to do. It would take some book-keeping to get the other outputs C and ja, though. I work this out to be
[~,iia,jja]=unique(fliplr(A),'stable');
ia=length(A)+1-fliplr(iia(:).');
C=A(ia);
iflip=length(ia):-1:1;
ja=iflip(jja);
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!