Borrar filtros
Borrar filtros

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

24 visualizaciones (últimos 30 días)
Germán
Germán el 13 de Dic. de 2012
Comentada: QIAO WANG el 30 de Abr. de 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')

Respuesta aceptada

Matt J
Matt J el 13 de Dic. de 2012
Editada: Matt J el 14 de Dic. de 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 comentario
QIAO WANG
QIAO WANG el 30 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 14 de Dic. de 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 comentario
Matt J
Matt J el 14 de Dic. de 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);

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by