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:
- First array (
arr1[0]
,arr2[0]
) are x-axis values - Second array (
arr1[1]
,arr2[1]
) are y-axis values
I would like to find the xy pairs that match between the two arrays.
Some clarifications:
arr1
andarr2
will not necessarily be of equal length. They could be different lengths- X value and Y value pairs could be in any ordering. Sorting or alignment between arrays is not expected
- Duplicates of the same X value and Y value pairs will not occur in the same array
In the above examples, the pairs that are the same between the two arrays are:
- X = 32, Y = 449
- X = 32, Y = 452
- X = 32, Y = 453
- X = 39, Y = 463
I've tried to use np.intersect1d
and some other functions I found.
CodePudding user response:
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')])
CodePudding user response:
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
. Here's how I would do:
np.array(list(
set(zip(arr1[0,:], arr1[1,:])).intersection(
set(zip(arr2[0,:], arr2[1,:])))
))
or wjandrea's more concise version:
np.array(list(
set(zip(*arr1)).intersection(zip(*arr2))
))