Approach No. 1
One approach would use np.in1d-
m_a = np.in1d(a,b)
I = np.flatnonzero(m_a)
J = np.flatnonzero(np.in1d(b, a[m_a]))
Example input, output -
In [367]: a
Out[367]: array([1, 2, 3, 4])
In [368]: b
Out[368]: array([3, 5, 6, 4])
In [370]: I
Out[370]: array([2, 3])
In [371]: J
Out[371]: array([0, 3])
Approach No. 2
Another direct but large amount of memory will be with broadcasting-
I,J = np.nonzero(a[:,None] == b)
Approach No. 3
In the absence of duplicates inside the input arrays we can use np.searchsorted. There are two options: one for sorting aand one for general a.
Option No. 1: For sorting a-
idx = np.searchsorted(a, b)
idx[idx==a.size] = 0
mask = a[idx] == b
I = np.searchsorted(a,b[mask])
J = np.flatnonzero(mask)
№ 2: argsort a -
sidx = a.argsort()
a_sort = a[sidx]
idx = np.searchsorted(a_sort, b)
idx[idx==a.size] = 0
mask = a_sort[idx] == b
I = sidx[np.searchsorted(a_sort,b[mask])]
J = np.flatnonzero(mask)