I have two arrays of different sizes and want to determine where elements of one array can be found in the other array. I would like to be able to allow for a tolerance between elements. The goal would be something like this:
array1 = [1, 2, 3, 4, 5, 6]
array2 = [2, 8, 1.00001, 1.1]
places = [mystery function](array1, array2, tolerance = 0.001)
returning indices places = [0,1]
in array1
.
The closest I can get is with np.isin
, which allows for arrays of different sizes and orders, but not the tolerance. (have also tried np.allclose
but the shape and order mismatch is an issue there). Of course this can be done with a loop, but my actual arrays are thousands of elements long so a loop is not practical.
(it also doesn't have to be a numpy function--really just anything more efficient than a loop)
Thanks in advance for the help!
With custom function to find the absolute difference between 2 arrays and positions which fit the given tolerance:
array1 = np.array([1, 2, 3, 4, 5, 6])
array2 = np.array([2, 8, 1.00001, 1.1])
tolerance = 0.001
def argwhere_close(a, b, tol):
return np.where(np.any(np.abs(a - b[:, None]) <= tolerance, axis=0))[0]
print(argwhere_close(array1, array2, tolerance))
[0 1]