Is there Python equivalent to MATLAB unique?

Viewed 5772

I am rewriting one code

nd = unique(nd,'rows');

Documentation from The Mathworks

C = unique(A,'rows') treats each row of A as a single entity and returns the unique rows of A. The rows of the array C are in sorted order.

Is there something like this in Python or I have to use sort?

2 Answers

Python:

a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4], [2, 3, 4],[2, 3, 5],[1, 0, 0],[2,2,5],[2,3,5]])
c, ia, ic= np.unique(a,return_index=True,return_inverse=True, axis=0)

This will give the same result as the unique function of Matlab. The only difference is the base index which starts from 0 in Python.

Related