Here's an alternative using np.searchsorted -
def in1d_alternative_2D(npArr, arr):
idx = np.searchsorted(arr, npArr.ravel())
idx[idx==len(arr)] = 0
return arr[idx].reshape(npArr.shape) == npArr
It assumes arr to be sorted. If it's not, we need to sort and then use the posted method.
Sample run -
In [90]: npArr = np.array([[1, 4, 5, 5, 3],
...: [2, 5, 6, 6, 1],
...: [0, 0, 1, 0, 0],
...: [3, 3, 2, 14, 3]])
...:
...: arr = np.array([3,5,8])
...:
In [91]: in1d_alternative_2D(npArr, arr)
Out[91]:
array([[False, False, True, True, True],
[False, True, False, False, False],
[False, False, False, False, False],
[ True, True, False, False, True]], dtype=bool)
In [92]: npArr[in1d_alternative_2D(npArr, arr)] = 0
In [93]: npArr
Out[93]:
array([[ 1, 4, 0, 0, 0],
[ 2, 0, 6, 6, 1],
[ 0, 0, 1, 0, 0],
[ 0, 0, 2, 14, 0]])
Benchmarking against numpy.in1d
Equivalent solution using np.in1d would be :
np.in1d(npArr, arr).reshape(npArr.shape)
Let's time our proposed one against it and also verify results for the sizes mentioned in the question.
In [85]: # (4096, 4096) shaped 'npArr' and search array 'arr' of 1000 elems
...: npArr = np.random.randint(0,10000,(4096,4096))
...: arr = np.sort(np.random.choice(10000, 1000, replace=0 ))
...:
In [86]: out1 = np.in1d(npArr, arr).reshape(npArr.shape)
...: out2 = in1d_alternative_2D(npArr, arr)
...:
In [87]: np.allclose(out1, out2)
Out[87]: True
In [88]: %timeit np.in1d(npArr, arr).reshape(npArr.shape)
1 loops, best of 3: 3.04 s per loop
In [89]: %timeit in1d_alternative_2D(npArr, arr)
1 loops, best of 3: 1 s per loop