0

I'm trying to transfer MATLAB code to Python, but couldn't find an implementation to replace the function unique in MATLAB.

In the original MATLAB code, unique() is used as:

[c,ia,ic]=unique(a,'stable')

c is an array with unique elements and has the same sequence as a.

I know in numpy there is an implementation

c,ic = np.unique(a,return_reverse=True)

This function returns a sorted array c, but I prefer c to have the original sequence as a with unique elements.

And ic is what I really want. I'm trying to use ic to locate some value in A.

beaker
  • 16,331
  • 3
  • 32
  • 49
BEJ
  • 1
  • 1
  • If you read the [documentation](https://numpy.org/doc/stable/reference/generated/numpy.unique.html), there are two parameters that give you the `ia` and `ic` arrays. – beaker Jul 02 '21 at 14:37
  • thanks for the comment,but `ia` and `ic` returned by `np.unique()` is based on a sorted array,which is not what I want.I want the selected unique array to keep its original order and `ic` based on this array. – BEJ Jul 04 '21 at 14:23
  • `C2, ic = np.unique(a, return_inverse=True)` gives me the same behavior in both MATLAB and Python (accounting for 1- vs 0-based indexing). Please post an example if you are seeing something different. – beaker Jul 04 '21 at 15:30
  • I guess you set `a` as an ordered array or used `unique(a,'legacy')`instead of `unique(a,'stable')`in MATLAB,it will return different `c`.Please try `a=[2,1,0,4,1]`,when I use `c, ic = np.unique(a, return_inverse=True)`,`c=[0,1,2,4]`which is in order,and`ic = [2,1,0,3,1]`which is based on the ordered array `c`;When I use MATLAB,`[c,ia,ic] = unique(a,'stable')`,`c=[2,1,0,4]`,which is the same sequence in `a`,and `ic = [1,2,3,4,2]`,different from the result that np.unique give. – BEJ Jul 05 '21 at 08:05
  • What behavior are you trying to achieve? Your original question says that you are trying to get `numpy.unique()` to behave the same way as MATLAB's `unique(...,'legacy')`. In your comment, it seems you've switched to wanting to make MATLAB's `unique(...,'stable')` behave like `numpy.unique()`. Which is it? Please update your question with your code (it's impossible to read in comments) and your desired result. – beaker Jul 05 '21 at 17:30
  • Sorry for my vague expression in the question,I've changed the question to make it clear.And `unique(...,'stable')` is what I want. – BEJ Jul 06 '21 at 06:21
  • It would also be helpful if you told us how you plan to use `ic`. Depending on what you want to do, there may be a different answer. – beaker Jul 06 '21 at 15:15
  • As far as I know, there isn't a way to tell `np.unique()` to not sort the unique values. You can sort the `return_index` array (`ia`) to create the unsorted list of values as in [this answer](https://stackoverflow.com/questions/12926898/numpy-unique-without-sort/12926989#12926989). From there you can use `np.searchsorted()` to produce the unsorted `ic` array [like this](https://stackoverflow.com/questions/8251541/numpy-for-every-element-in-one-array-find-the-index-in-another-array/8251668#8251668). It's a bit cumbersome, but it works. – beaker Jul 07 '21 at 16:27
  • Thanks for your recommandation,I also find [this](https://stackoverflow.com/q/8251541/14201358) works. – BEJ Jul 08 '21 at 08:59

0 Answers0