One way to achieve this.
If any row of arr1
were not found in arr2
, then at that location in pos
will have value -1
for simplicity.
This heavily uses numpy broadcasting and indexing. Feel free to ask for further clarifications.
Original example:
import numpy as np
arr1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[4, 5, 6],
[1, 2, 3]])
arr2 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
inds = arr1 == arr2[:, None]
row_sums = inds.sum(axis = 2)
i, j = np.where(row_sums == 3) # Check which rows match in all 3 columns
pos = np.ones(arr1.shape[0], dtype = 'int64') * -1
pos[j] = i
pos
array([0, 1, 2, 1, 0])
Example 2:
import numpy as np
arr1 = np.array([[1, 2, 4],
[4, 5, 6],
[7, 8, 9],
[4, 1, 6],
[1, 2, 3]])
arr2 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
inds = arr1 == arr2[:, None]
row_sums = inds.sum(axis = 2)
i, j = np.where(row_sums == 3)
pos = np.ones(arr1.shape[0], dtype = 'int64') * -1
pos[j] = i
pos
array([-1, 1, 2, -1, 0])
If you have more number of columns just change the line i, j = np.where(row_sums == 3)
to i, j = np.where(row_sums == arr1.shape[1])
.