Given numpy arrays *a* and *b*, it is fairly straightforward find the indices of array *a* whose elements overlap with the elements of array *b* using the function numpy.in1d(). However, what if you want to also find the indices associated with elements in *b* that overlap with *a*? I’m unable to find a good solution to this, so I wrote a small function to do this. It does brute force matching after getting all the common elements of array *a* and *b*. If anyone knows of a better way, please let me know. The code snippet is below.

import numpy as np def overlap(a, b): # return the indices in a that overlap with b, also returns # the corresponding index in b only works if both a and b are unique! # This is not very efficient but it works bool_a = np.in1d(a,b) ind_a = np.arange(len(a)) ind_a = ind_a[bool_a] ind_b = np.array([np.argwhere(b == a[x]) for x in ind_a]).flatten() return ind_a,ind_b

Usage:

import overlap a = np.array([1,2,4,5]) b = np.array([4,6,10,9,1]) ind_a, ind_b = overlap.overlap(a,b)