Reputation: 339
I want to compare the elements of two 3D numpy arrays of different lengths. The goal is, to find overlapping elements in the two arrays.
All functions I found so far, rely on the two arrays being of the same lengths.
Is there an efficient way to do compare the 2D-elements (for loops will be very inefficient, since each array has tens of thousands of elements)?
Upvotes: 0
Views: 2547
Reputation: 231325
Here a few ways of comparing 2 1d arrays:
In [325]: n=np.arange(0,10)
In [326]: m=np.arange(3,9)
In [327]: np.in1d(n,m)
Out[327]: array([False, False, False, True, True, True, True, True, True, False], dtype=bool)
In [328]: np.in1d(m,n)
Out[328]: array([ True, True, True, True, True, True], dtype=bool)
In [329]: n[:,None]==m[None,:]
Out[329]:
array([[False, False, False, False, False, False],
[False, False, False, False, False, False],
[False, False, False, False, False, False],
[ True, False, False, False, False, False],
[False, True, False, False, False, False],
[False, False, True, False, False, False],
[False, False, False, True, False, False],
[False, False, False, False, True, False],
[False, False, False, False, False, True],
[False, False, False, False, False, False]], dtype=bool)
and farenorth
s suggestion
In [330]: np.intersect1d(n,m)
Out[330]: array([3, 4, 5, 6, 7, 8])
In [331]: np.where(np.in1d(n,m))
Out[331]: (array([3, 4, 5, 6, 7, 8], dtype=int64),)
Upvotes: 1
Reputation: 10771
Is intersect1d
what you want? For example, if your arrays are a
and b
, you could simply do:
duplicates = np.intersect1d(a, b)
Upvotes: 0