I have a 2-dimensional ndarray
, the rows of which shall be scanned to check whether any one is equal to any other.
My first try actually works, but I feel it is not the optimal way. It takes time once the number of rows in the matrix approaches 1000.
My code is the following. X
is the aforementioned array, Y
also is a 2-dimensional ndarray
.
for i in range(X.shape[0]-1):
for j in range(i+1,X.shape[0]):
if (np.all( (X[i,:] == X[j,:] ), axis = 0 )):
Y[j,:] = Y[i,:]
#endif
#enddo
#enddo
I know that the nested loop is time consuming and should be avoided, but I could not find an alternative. List comprehension seems to me not suitable in that there is no need to save items.
The fact that the core of the procedure is the assignment operation Y[j,:] = Y[i,:]
, which is index-dependent, would lead me to exclude a list comprehension-like solution.
Question then is: is there a more efficient way to code such a search exploiting numpy
vectorization?