Reputation: 3872
I have two large 2D arrays (3x100,000) corresponding to 3D coordinates and I would like to find index of each correspondence.
An example:
mat1 = np.array([[0,0,0],[0,0,0],[0,0,0],[10,11,12],[1,2,3]]).T
mat2 = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]).T
So here I need to obtain indexes of 3 and 0. And I need to find each correspondence on around 100,000 coordinates. Is there a specific function in Python to do this work? Apply a for loop could be probl
res = [3,0]
To sum up, my need:
Upvotes: 3
Views: 414
Reputation: 221704
We can use Cython-powered kd-tree
for quick nearest-neighbor lookup -
In [77]: from scipy.spatial import cKDTree
In [78]: d,idx = cKDTree(mat2.T).query(mat1.T, k=1)
In [79]: idx[np.isclose(d,0)]
Out[79]: array([3, 0])
Upvotes: 3