【问题标题】:How to calculate distance between two points in 3D?如何计算3D中两点之间的距离?
【发布时间】:2023-12-09 06:22:01
【问题描述】:

我有两个列表。每个列表有三行。每个列表的坐标系从上到下为 (x,y,z)。我尝试使用数组,但它没有用。这是我的代码。

import numpy as np
p1 = np.array([list(marker_11_x['11:-.X']), list(marker_11_y['11:-.Y']), 
list(marker_11_z['11:-.Z']) ])
p2 = np.array([list(original_x['13:-.X']), list(original_y['13:-.Y']), 
list(original_z['13:-.Z'])])

squared_dist = np.sum(((p1[0]-p2[0])**2+(p1[1]-p2[1] )**2+(p1[3]-p2[3] )**2), 
axis=0)
dist = np.sqrt(squared_dist)

list A = [-232.34, -233.1, -232.44, -233.02, -232.47, -232.17, -232.6, -232.29, -231.65]
[-48.48, -49.48, -50.81, -51.42, -51.95, -52.25, -52.83, -53.63, -53.24]
[-260.77, -253.6, -250.25, -248.88, -248.06, -247.59, -245.82, -243.98, -243.76]

List B = [-302.07, -302.13, -303.13, -302.69, -303.03, -302.55, -302.6, -302.46, -302.59]
[-1.73, -3.37, -4.92, -4.85, -5.61, -5.2, -5.91, -6.41, -7.4]
[-280.1, -273.02, -269.74, -268.32, -267.45, -267.22, -266.01, -264.79, -264.96]

TypeError Traceback(最近一次调用最后一次) pandas._libs.index.IndexEngine.get_loc() 中的 pandas_libs\index.pyx

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError:需要一个整数

在处理上述异常的过程中,又发生了一个异常:

KeyError Traceback(最近一次调用最后一次) 在 () 1 将 numpy 导入为 np 2 p1 = np.array([list(marker_11_x['11:-.X']), list(marker_11_y['11:-.Y']), list(marker_11_z['11:-.Z'])] ) ----> 3 p2 = np.array([list(original_x['13:-.X']), list(original_y['13:-.Y']), list(original_z['13:-. Z'])]) 4 5 squared_dist = np.sum(((p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[3]-p2[3])* *2), 轴=0)

E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in getitem(self,key) 第764章 765尝试: --> 766 结果 = self.index.get_value(self, key) 767 768如果不是is_scalar(结果):

E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key) 3101 尝试: 第3102章 -> 3103 tz=getattr(series.dtype, 'tz', None)) 3104 除了 KeyError 作为 e1: 3105 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

KeyError: '13:-.X'

【问题讨论】:

  • 您必须比“它不起作用”更具体才能获得任何有用的帮助。如果有错误,请发布完整的回溯。如果结果有误,请发布您得到的结果和预期结果。见minimal reproducible example
  • 感谢您的建议。我是新来的。

标签: python arrays distance


【解决方案1】:

代码和公式是这样的:

def distance_finder(one,two) :
    [x1,y1,z1] = one  # first coordinates
    [x2,y2,z2] = two  # second coordinates

    return (((x2-x1)**2)+((y2-y1)**2)+((z2-z1)**2))**(1/2)

【讨论】:

  • 除非在不寻常的情况下,这可能应该使用 NumPy 数组来实现,并且如果可能的话利用预构建的工具,例如 scipy.spatial.distance.cdist
  • 这是错误的,该功能无法按此处所述运行。如果您希望此函数适用于两个列表,则需要将点作为函数的一部分:def distance_finder(one,two) : [x1,y1,z1] = one # first coordinates [x2,y2,z2] = two # second coordinates dist = (((x2-x1)**2)+((y2-y1)**2)+((z2-z1)**2))**(1/2) return dist@StevenC.Howell
最近更新 更多