Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find correspondence between 2 arrays of coordinates

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:

enter image description here

like image 622
user3601754 Avatar asked Nov 23 '25 04:11

user3601754


1 Answers

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])
like image 86
Divakar Avatar answered Nov 25 '25 18:11

Divakar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!