MATLAB Answers


unique function in matlab

Asked by zhang on 31 Jul 2012
Latest activity Commented on by Walter Roberson
on 21 Jan 2017
Hi guys
In the version of R2012a, the unique function has a 'stable' property. Please see the link:
A = [9 2 9 5]; [C, ia, ic] = unique(A,'stable')
C =
9 2 5
ia =
ic =
But my matlab version is R2010a, it seems that the unique function in R2010a does not support 'stable'. How can I get the stable result?


Show 1 older comment
Paul Cisek on 21 Jan 2017
Or even better - Mathworks could stop pointlessly changing the functionality of their software ;-)
on 21 Jan 2017
It's hardly pointless, and it was the addition of a new option to an existing function which is perfectly fine.
Code written for R2010a unique works exactly the same in R2012a
Walter Roberson
on 21 Jan 2017
Paul Cisek: how would you envision that working? You seem to be implying that Mathworks should only make bug fixes and performance improvements that do not change the output at all (even at the round-off level.) Does that sound like a route to financial viability?

Sign in to comment.


2 Answers

Albert Yam
Answer by Albert Yam
on 31 Jul 2012

I needed this as well, for R2007a
[uA a b] = unique(A,'first');
C = A(sort(a));

  1 Comment

Sean de Wolski
on 31 Jul 2012
I like this better than mine.

Sign in to comment.

Sean de Wolski
Answer by Sean de Wolski
on 31 Jul 2012

The 'stable' option was introduced in R2012a.
One way to achieve something similar with an older release is to first sort the vector, then unique it and then index back into the original.
A = [9 2 9 5 5 4 4 1 1 1];
[As,idx] = sort(A);
[U,ij] = unique(As,'first');


Sign in to comment.