简体   繁体   English

如何使用python计算一组3d点之间的距离?

[英]How can I calculate the distances of a set of 3d points with each other using python?

I have a set of 3D points, where I need to find the distances of every point with all other points. 我有一组3D点,需要在其中找到每个点与所有其他点的距离。 So far I came up with the code as below to calculate the distances between two consecutive (in order of the array) points but can't figure out how can I calculate the distance of each point with all other points. 到目前为止,我想出了以下代码来计算两个连续(按数组顺序)的点之间的距离,但无法弄清楚如何计算每个点与所有其他点的距离。 Every point will have 9 distances (with 9 other points), thus 10 points have a total of 45 distances (half of 90). 每个点将有9个距离(还有9个其他点),因此10个点总共有45个距离(一半为90)。 So far I have found 9 distances only. 到目前为止,我仅发现9个距离。 Any idea how can get all the distances efficiently using python? 知道如何使用python有效地获得所有距离吗?

import numpy as np
import scipy as sp

rij_mat = np.zeros((9,1),dtype=np.float32) """created a 9x1 matrix for storing 9 distances for 10 points"""

npoints = 10

x = sp.randn(npoints,3)

print "here are the 3-d points..."
print x

for i in xrange(npoints-1):

 rij_mat[i] = np.linalg.norm(x[i+1]-x[i])

print "the distances are..."
print rij_mat

My output right now is something like this: 我现在的输出是这样的:

here are the 3-d points...
[[-0.89513316  0.05061497 -1.19045606]
 [ 0.31999847 -0.68013916 -0.14576028]
 [ 0.85442751 -0.64139512  1.70403995]
 [ 0.55855264  0.56652717 -0.17086825]
 [-1.22435021  0.25683649  0.85128921]
 [ 0.80310031  0.82236372 -0.40015387]
 [-1.34356018  1.034942    0.00878305]
 [-0.65347726  1.1697195  -0.45206805]
 [ 0.65714623 -1.07237429 -0.75204526]
 [ 1.17204207  0.89878048 -0.54657068]]
the distances are...
[[ 1.7612313 ]
 [ 1.92584431]
 [ 2.24986649]
 [ 2.07833028]
 [ 2.44877243]
 [ 2.19557977]
 [ 0.84069204]
 [ 2.61432695]
 [ 2.04763007]]

How about two loops instead of one? 两个循环而不是一个循环怎么样?

distances = []
for i in xrange(npoints-1):
    for j in range(i+1, npoints):
        distances.append(np.linalg.norm(x[i]-x[j])

For each of your npoints points, there are exactly npoints - 1 other points to compare with, for distance. 对于您的每个npoints点,确切都有npoints - 1要与其他npoints - 1个点进行比较, npoints - 1距离。 And, the distance between x[m] and x[n] is the same as the distance between x[n] and x[m] , so that cuts the total number of distances in half. 并且, x[m]x[n]之间的距离与x[n]x[m]之间的距离相同,因此将距离的总数减少了一半。 The itertools package has a nice way to handle this: itertools软件包有一个很好的方法来处理此问题:

import numpy as np
import scipy as sp
import itertools as its

npoints = 10
nCombos = (npoints * (npoints - 1))/2
x = sp.randn(npoints,3)
rij_mat = np.zeros(nCombos)

ii = 0
for i1, i2 in its.combinations(range(npoints), 2):
    rij_mat[ii] = np.linalg.norm(x[i1]-x[i2])
    ii += 1

print "the distances are..."
print rij_mat

If you're a really careful person, you might check at the end that ii == nCombos . 如果您是一个非常谨慎的人,则可以在最后检查ii == nCombos

Since you called your output matrix rij_mat , maybe you intended it to be a 2-dimensional matrix? 由于您调用了输出矩阵rij_mat ,也许您希望将其作为二维矩阵? Then you'd want something like: 然后,您需要类似:

import numpy as np
import scipy as sp
import itertools as its

npoints = 10

x = sp.randn(npoints,3)
rij_mat = np.zeros((npoints, npoints))

for i1, i2 in its.combinations(range(npoints), 2):
    rij_mat[i2, i1] = rij_mat[i1, i2] = np.linalg.norm(x[i1]-x[i2])

print "the distances are..."
print rij_mat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用python从一组点中选择相等距离的点 - How can i select equal distances points from a set of points using python 如何计算数组中 3D 坐标的距离 - How to calculate distances in 3D coordinates in an array 如何平滑一组 3D 点? - How can I smooth a set of 3D points? Python:如何重新采样由点作为样条等距离给出的 3d 曲线? - Python: How to resample a 3d curve given by points as spline in equal distances? 如何提取 python 中树状图中点之间的距离? - How can I extract the distances between points within a dendogram in python? 如何使用python在opencv中计算由3D点组成的弧的弧长? - how to calculate arc length of the arc consisting of 3D points in opencv using python? 如何在 3d 空间中定义矢量点的自定义类并计算它们的中心(平均)点? - How can I define a custom class of vector points in 3d space and calculate their center(mean) point? 如何获得轮廓 plot 和 3D plot 使用 z1587383CF8C21507D06FB 的 zDDDA 和 a set5ZEF3 列的起始点来表示? - How can I obtain a contour plot and a 3D plot using Matplotlib starting from a set of 3 columns representing x,y and z points? 如何使用立体三角测量从2D图像点计算3D对象点? - How to calculate 3D object points from 2D image points using stereo triangulation? 使用python中的内置函数查找3d距离 - Finding 3d distances using an inbuilt function in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM