[英]Faster way to calculate distance between two 3D points
I have 4 lists of length 160000 as s, x, y, z.我有 4 个长度为 160000 的列表,分别为 s、x、y、z。 I made a list(points) of 3d array of x,y,z.
我列出了 x、y、z 的 3d 数组的列表(点)。 I need to find the distance between all combinations of points for criteria and match the index of the points to that of list s, so that I get the s value of 2 points which satisfy it.
我需要找到标准点的所有组合之间的距离,并将点的索引与列表 s 的索引匹配,以便获得满足它的 2 个点的 s 值。 I'm using the code below.
我正在使用下面的代码。 Is there any faster way to do this?
有没有更快的方法来做到这一点?
import numpy as np
points = []
for i in range(len(xnew)):
a = np.array((xnew[i],ynew[i],znew[i]))
points.append(a)
for i in range(len(points)):
for j in range(len(points)):
d = np.sqrt(np.sum((points[i] - points[j]) ** 2))
if d <= 4 and d >=3:
print(s[i],s[j],d)
Idea is to use cdist and np.where to vectorize the processing想法是使用cdist和np.where来向量化处理
Code代码
import numpy as np
import scipy.spatial.distance
# Distance between all pairs of points
d = scipy.spatial.distance.cdist(points, points)
# Pairs within threshold
indexes = np.where(np.logical_and(d>=3, d<=4))
for i, j in indexes:
if i < j: # since distance is symmetric, not reporting j, i
print(s[i],s[j],d[i][j])
If d Matrix is too large to fit into memory, find the distance of each point to all other points如果 d 矩阵太大而无法放入 memory,求每个点到所有其他点的距离
for i in range(len(points)):
# Distance from point i to all other points
d = scipy.spatial.distance.cdist(points,[points[i]])
# Points within threshold
indexes = np.where(np.logical_and(d>=3, d<=4))
for ind in indexes:
if ind.size > 0:
for j in ind:
if i < j: # since distance is symmetric, not reporting j, i
print(s[i], s[j], d[j][0])
Tests测试
points = [
[1, 2, 3],
[1.1, 2.2, 3.3],
[4, 5, 6],
[2, 3, 4]
]
s = [0, 1, 2, 3]
Output (both methods) Output (两种方法)
2 3 3.4641016151377544
points = np.array([x, y, z]).T
t1, t2 = np.triu_indices(len(points), k= 1) # triangular indices
p1 = points[t1]
p2 = points[t2]
d = p1 - p2 # displacements from p1 to p2
d = np.linalg.norm(d, axis= -1) # distances from p1 to p2
mask = (3 <= d) & (d <= 4)
indx = np.where(mask) # indices where distance is between 4 and 3
ans = np.array([ s[t1[i]], s[t2[i]], d[i] ]).T
test run:测试运行:
n = 10
x = np.random.randint(10, size= [n]) # dummy data
y = np.random.randint(10, size= [n])
z = np.random.randint(10, size= [n])
s = np.random.randint(10, size= [n])
after running the above code运行上述代码后
points
>>> array([
[9, 3, 5],
[7, 8, 1],
[0, 0, 2],
[6, 7, 2],
[4, 4, 3],
[8, 0, 9],
[5, 2, 6],
[0, 8, 9],
[2, 6, 9],
[4, 8, 4]])
s
>>> array([4, 2, 9, 9, 8, 2, 7, 6, 0, 5])
for e in ans:
print(*e)
>>> 9.0 8.0 3.7416573867739413
9.0 5.0 3.0
8.0 7.0 3.7416573867739413
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.