Find matching points in 2 separate numpy arrays

I have two arrays of different sizes containing 3D points. I would like to effectively compare two arrays and find points that match and ultimately return a simple number of matching points.

pA=[[0,0,0],[0,1,0],[1,2,4],[10,3,4],[1,20,1],[5,3,2]]
pB=[[14,1,0],[1,2,4],[1,20,1],[15,1,0]]

#returns 2

I currently have a sloppy loop that does the trick, but it is not very convenient for work, which is a problem, given that I am trying to match many pairs of arrays with a lot of points

t= np.array([pA[x]==pB for x in range(len(pA))]).sum(2)
print np.sum(t==3)

I'm just not sure how to effectively compare two multidimensional arrays of different sizes. And then, how to do a few iterations for a lot of pairs.

EDIT

I found a bit of a workaround, which quickly combines arrays, creates a unique version of the array and then compares the lengths of two arrays.

pts=np.concatenate((pA,pB),axis=0)
pts2 = np.unique(pts.view([('', pts.dtype)]*pts.shape[1]))
return len(pts)-len(pts2)
+4
2

, , Scipy kdtree:

from scipy.spatial import cKDTree

pA=[[0,0,0],[0,1,0],[1,2,4],[10,3,4],[1,20,1],[5,3,2]]
pB=[[14,1,0],[1,2,4],[1,20,1],[15,1,0]]

kdtree = cKDTree(pA)
dists, inds = kdtree.query(pB, distance_upper_bound=1e-5)
result = (dists == 0).sum()
+3

, numpy. , numpy. , . diff, , np.all(...==0,1). , .

-

import numpy as np

# Inputs
pA=[[0,0,0],[0,1,0],[1,2,4],[10,3,4],[1,20,1],[5,3,2]]
pB=[[14,1,0],[1,2,4],[1,20,1],[15,1,0]]

# Form concatenate array of pA and pB
pts = np.concatenate((pA,pB),axis=0)

# Sort pts by rows
spts = pts[pts[:,1].argsort(),]

# Finally get counts by DIFFing along rows and counting all zero rows
counts = np.sum(np.diff(np.all(np.diff(spts,axis=0)==0,1)+0)==1)

-

In [152]: counts
Out[152]: 2

, . , -

# Inputs
pA=[[0,0,0],[0,1,0],[1,2,4],[10,3,4],[1,20,1],[5,3,2],[1,2,4]]
pB=[[14,1,0],[1,2,4],[1,20,1],[15,1,0],[1,2,4]]

- 2, .

, , -

counts = np.sum(np.all(np.diff(spts,axis=0)==0,1))
+1

All Articles