Filter löschen
Filter löschen

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

24 Ansichten (letzte 30 Tage)
Germán
Germán am 13 Dez. 2012
Kommentiert: QIAO WANG am 30 Apr. 2019
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')

Akzeptierte Antwort

Matt J
Matt J am 13 Dez. 2012
Bearbeitet: Matt J am 14 Dez. 2012
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 Kommentar
QIAO WANG
QIAO WANG am 30 Apr. 2019
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 14 Dez. 2012
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 Kommentar
Matt J
Matt J am 14 Dez. 2012
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by