I have two arrays (A,B)
containing: ID
, x
, y
, z
of the same number of points but slightly differents.
I would like to have an array in where each row has the ID x y z
of the two nearest points of the two arrays.
At the moment I have this:
import numpy as np
from scipy.spatial import cKDTree
A = np.loadtxt('A.txt')
B = np.loadtxt('B.txt')
tree = cKDTree( B[:,[1,2,3]] )
d, inds = tree.query( A[:,[1,2,3]], k=1, p=np.inf, eps=0.0)
A_new = A[inds]
xyz_near = np.hstack(( B[:,0:4], A_new[:,0:4] ))
But the array xyz_near
does not contain the right couple (IDB xB yB zB DIA xA yA zA):
12587 18.0445 0.0784 -7.4705 3422 18.0444 0.0403 -7.4639
12588 18.0447 0.0783 -7.2231 3421 18.0446 0.0402 -7.2164
12589 18.0450 0.0781 -6.9756 7764 18.0461 0.0400 -5.9785
12590 18.0452 0.0779 -6.7281 7765 18.0464 0.0399 -5.7310
12591 18.0454 0.0777 -6.4805 7766 18.0467 0.0399 -5.4835
12592 18.0457 0.0775 -6.2329 7767 18.0470 0.0398 -5.2359
12593 18.0459 0.0773 -5.9852 7768 18.0473 0.0398 -4.9884
As you can see the first two rows are right but not the next.
If I do the same thing in matlab with dsearchn
(IDB xB yB zB DIA xA yA zA):
12587 18.0445 0.0784 -7.4705 3422 18.0444 0.0403 -7.4639
12588 18.0447 0.0783 -7.2231 3421 18.0446 0.0402 -7.2164
12589 18.0450 0.0781 -6.9756 3420 18.0448 0.0402 -6.9688
12590 18.0452 0.0779 -6.7281 3419 18.0450 0.0401 -6.7212
12591 18.0454 0.0777 -6.4805 3418 18.0453 0.0401 -6.4737
12592 18.0457 0.0775 -6.2329 3417 18.0455 0.0400 -6.2261
12593 18.0459 0.0773 -5.9852 3416 18.0458 0.0400 -5.9785
that is right.
I have tried to change p
to 1, 2 and np.inf
but this gives the same result.
Files:
A.txt: http://pasted.co/8c5b6156
B.txt: http://pasted.co/28a228e6
Thanks
UPDATE: Even with the fix suggested by ergo_ I got:
12587 18.0445 0.0784 -7.4705 7758 18.0448 0.0403 -7.4639
12587 18.0445 0.0784 -7.4705 3422 18.0444 0.0403 -7.4639
12588 18.0447 0.0783 -7.2231 3421 18.0446 0.0402 -7.2164
12588 18.0447 0.0783 -7.2231 7759 18.0450 0.0402 -7.2163
12589 18.0450 0.0781 -6.9756 7760 18.0452 0.0402 -6.9688
12589 18.0450 0.0781 -6.9756 3420 18.0448 0.0402 -6.9688
12590 18.0452 0.0779 -6.7281 3419 18.0450 0.0401 -6.7212
12590 18.0452 0.0779 -6.7281 7761 18.0454 0.0401 -6.7212
12591 18.0454 0.0777 -6.4805 7762 18.0456 0.0401 -6.4736
12591 18.0454 0.0777 -6.4805 3418 18.0453 0.0401 -6.4737
So it considers multiple times the same points.