Does any one know how to get unique elements row wise in a matrix. For e.g. input matrix may be like:
a = [[1,2,1,3,4,1,3],
[5,5,3,1,5,1,2],
[1,2,3
The fastest way should be to set all duplicates to zero using sort and diff:
def row_unique(a):
unique = np.sort(a)
duplicates = unique[:, 1:] == unique[:, :-1]
unique[:, 1:][duplicates] = 0
return unique
This is about 3 times as fast as the unutbu's solution on my computer:
In [26]: a = np.random.randint(1, 101, size=100000).reshape(1000, 100)
In [27]: %timeit row_unique(a)
100 loops, best of 3: 3.18 ms per loop
In [28]: %timeit using_complex(a)
100 loops, best of 3: 15.4 ms per loop
In [29]: assert np.all(np.sort(using_complex(a)) == np.sort(row_unique(a)))