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
You can do something like this:
def solve(arr):
n = arr.shape[1]
new_arr = np.empty(arr.shape)
for i, row in enumerate(arr):
new_row = np.unique(row)
new_arr[i] = np.hstack((new_row, np.zeros(n - len(new_row))))
return new_arr
This is around 4X times faster than OP's current code for 1000 X 1000 array:
>>> arr = np.arange(1000000).reshape(1000, 1000)
>>> %timeit b = map(uniqueRowElements, arr); b = np.asarray(b)
10 loops, best of 3: 71.2 ms per loop
>>> %timeit solve(arr)
100 loops, best of 3: 16.6 ms per loop