在Python中以最快的方式找到3D中给定点的最接近点 [英] Fastest way to find the closest point to a given point in 3D, in Python
本文介绍了在Python中以最快的方式找到3D中给定点的最接近点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以可以说我在A中有10,000点,在B中有10,000点,并且想找出每个B点中A中最接近的点.
So lets say I have 10,000 points in A and 10,000 points in B and want to find out the closest point in A for every B point.
当前,我只是遍历B和A中的每个点,以找出距离最近的那个.即.
Currently, I simply loop through every point in B and A to find which one is closest in distance. ie.
B = [(.5, 1, 1), (1, .1, 1), (1, 1, .2)]
A = [(1, 1, .3), (1, 0, 1), (.4, 1, 1)]
C = {}
for bp in B:
closestDist = -1
for ap in A:
dist = sum(((bp[0]-ap[0])**2, (bp[1]-ap[1])**2, (bp[2]-ap[2])**2))
if(closestDist > dist or closestDist == -1):
C[bp] = ap
closestDist = dist
print C
但是,我确定有更快的方法来执行此操作...有什么想法吗?
However, I am sure there is a faster way to do this... any ideas?
推荐答案
我通常使用 kd-tree 在这种情况下.
I typically use a kd-tree in such situations.
有一个 C ++实现已包装与SWIG并与BioPython捆绑在一起,易于使用.
这篇关于在Python中以最快的方式找到3D中给定点的最接近点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文