我有一個x,y,z點列表。使用下式找出在3-dPython中三維點之間的最小距離,平均距離和最大距離
import math
import numpy as np
point0 = x0, y0, z0
point1 = x1, y1, z1
dist = math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
def dist3d((x0, y0, z0), (x1, y1, z1)):
return math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
兩個點之間的距離我希望寫一個優化的循環和存儲的距離
點= [(472765.09,6191522.78,13.0),(472764.82, 6191524.09,9.0),(472763.8,6191525.68,8.0),(472764.07,6191524.39,16.0)]
dist01 = dist3d(test[0],test[1])
dist02 = dist3d(test[0],test[2])
dist03 = dist3d(test[0],test[2])
dist04 = dist3d(test[0],test[2])
dist12 = dist3d(test[1],test[2])
dist13 = dist3d(test[1],test[3])
dist23 = dist3d(test[2],test[3])
3d_l=[(dist01),(dist02),(dist03),(dist04),(dist12),(dist13),(dist23)]
3d_max =max(3d_l)
3d_min = min(3d_l)
3d_mean = np.average(3d_l)
我寫下面的函數(它不是最佳的)
def dist3d((x0, y0, z0), (x1, y1, z1)):
return math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
def dist_3d(obs):
dist_list = list()
while len(obs) != 1:
obs_g = [(obs[0], x) for x in obs[1:]]
dist_list.append([dist3d(obs_g[i][0], obs_g[i][1]) for i in xrange(len(obs_g))])
obs.pop(0)
return dist_list
points = [(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0), (472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)]
print dist_3d(points)
[[4.217700795331081, 5.922339064664832, 3.554222840244929], [2.1374049685457694, 7.046453008421205], [8.107835716151763]]
什麼是你的問題? –
伊夫感謝您的重播。我的問題是將這些代碼行轉換爲(高效)循環 –
是的,但是它不能做什麼?這樣的(雙)循環是微不足道的。順便說一句,你想要的平均數或中位數?這會造成很大的差異。 –