【问题标题】:Squared Distance vs Distance for measuring the distance between points平方距离与距离,用于测量点之间的距离
【发布时间】:2020-12-03 19:53:38
【问题描述】:

我正在使用以下代码来测量两个 3d 点之间的距离。

import numpy as np

o1 = [-0.1695483, 0.08880598, -0.676]
o2 = [0.31333363, 0.06976531, -0.84899998]
o3 = [0.09157476, 0.05987176, -0.77499998]
o4 = [0.36830484, 0.10634459, -0.84100002]

squared_dist = np.sum((np.array(o3) - np.array(o1)) ** 2, axis=0)
print(squared_dist)

dist = np.linalg.norm(np.array(o3) - np.array(o3))

print(dist)

找到两个 3d 点之间的距离最可取的是哪个?

【问题讨论】:

标签: python numpy


【解决方案1】:

3d 向量的距离是 distance = (x ** 2 + y ** 2 + z ** 2) ** 0.5 。所以 linalg.norm 是正确的。要获得相同的结果,您需要执行 distance = squared_dist ** 0.5 。同样在您的示例中,您将 o3 与 o1 进行比较,另一次将 o3 与 o4 进行比较

如果您将正确的向量相互比较并取和的平方根,您会得到相同的结果

import numpy as np

o1 = [-0.1695483, 0.08880598, -0.676]
o2 = [0.31333363, 0.06976531, -0.84899998]
o3 = [0.09157476, 0.05987176, -0.77499998]
o4 = [0.36830484, 0.10634459, -0.84100002]

squared_dist = np.sum((np.array(o3) - np.array(o1)) ** 2, axis=0)
print(squared_dist ** 0.5)

dist = np.linalg.norm(np.array(o3) - np.array(o1))

print(dist)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2012-01-27
    相关资源
    最近更新 更多