why can't I use unique with 'stable' and 'last'?
10 次查看(过去 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!