Here's one way -
(arr == val).all(axis=-1).argmax()
Sample run -
In [977]: arr
Out[977]:
array([[1, 1, 1],
[1, 0, 1],
[0, 0, 1]])
In [978]: val
Out[978]: array([1, 0, 1])
In [979]: (arr == val).all(axis=1).argmax()
Out[979]: 1
Might be more performant with views
-
# https://stackoverflow.com/a/44999009/ @Divakar
def view1D(a): # a is array
a = np.ascontiguousarray(a)
void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
return a.view(void_dt).ravel()
out = (view1D(arr) == view1D(val[None])).argmax()
Extension to n-dim cases
Extending to n-dim array cases would need few more steps -
def first_match_index_along_axis(arr, val, axis):
s = [None]*arr.ndim
s[axis] = Ellipsis
mask = val[np.s_[s]] == arr
idx = mask.all(axis=axis,keepdims=True).argmax()
shp = list(arr.shape)
del shp[axis]
return np.unravel_index(idx, shp)
Sample runs -
In [74]: arr = np.random.randint(0,9,(4,5,6,7))
In [75]: first_match_index_along_axis(arr, arr[2,:,1,0], axis=1)
Out[75]: (2, 1, 0)
In [76]: first_match_index_along_axis(arr, arr[2,1,3,:], axis=3)
Out[76]: (2, 1, 3)