Reputation: 751
I have two arrays:
arr1 = np.array((
np.array([ 32, 32, 32, 32, 32, 39], dtype=np.int64),
np.array([449, 451, 452, 453, 454, 463], dtype=np.int64)))
arr2 = np.array((
np.array([ 39, 34, 32, 32, 37, 32], dtype=np.int64),
np.array([463, 393, 453, 452, 261, 449], dtype=np.int64)))
In these 2D arrays, the:
arr1[0]
, arr2[0]
) are x-axis valuesarr1[1]
, arr2[1]
) are y-axis valuesI would like to find the xy pairs that match between the two arrays.
Some clarifications:
arr1
and arr2
will not necessarily be of equal length. They could be different lengthsIn the above examples, the pairs that are the same between the two arrays are:
I've tried to use np.intersect1d
and some other functions I found.
Upvotes: 2
Views: 755
Reputation: 15502
Since you need to find the intersection of couples, in my opinion it's better to use the set
data structure instead of numpy.array
:
np.array(list(
set(zip(*arr1)).intersection(zip(*arr2))
))
Upvotes: 0
Reputation: 3985
Use a structured array.
import numpy as np
# Define a dtype with x and y integers
arr1 = np.empty(6, dtype=[('x', int), ('y', int)])
arr2 = np.empty(6, dtype=[('x', int), ('y', int)])
# Add the data to the structured array
arr1['x'] = np.array([ 32, 32, 32, 32, 32, 39])
arr1['y'] = np.array([449, 451, 452, 453, 454, 463])
arr2['x'] = np.array([ 39, 34, 32, 32, 37, 32])
arr2['y'] = np.array([463, 393, 453, 452, 261, 449])
Use intersect1d:
>>> np.intersect1d(arr1, arr2)
array([(32, 449), (32, 452), (32, 453), (39, 463)],
dtype=[('x', '<i8'), ('y', '<i8')])
Upvotes: 3