MATLAB Answers

why can't I use unique with 'stable' and 'last'?

27 ビュー (過去 30 日間)
Germán
Germán 2012 年 12 月 13 日
コメント済み: QIAO WANG 2019 年 4 月 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 件のコメント

Sign in to comment.

採用された回答

Matt J
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
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.

Sign in to comment.

その他の回答 (1 件)

Image Analyst
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
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);

Sign in to comment.

製品


Translated by