*更新*为两个二维阵列之间的距离创建一个数组

时间:2016-07-19 01:37:54

标签: python arrays numpy

所以我有两个有x,y,z坐标的数组。我只想尝试应用3D距离公式。问题是,我找不到构成每列中具有多个值的数组并发出数组的帖子。

print MW_FirstsubPos1 

[[  51618.7265625   106197.7578125    69647.6484375 ]
 [  33864.1953125    11757.29882812   11849.90332031]
 [  12750.09863281   58954.91015625   38067.0859375 ]
 ..., 
 [  99002.6640625    96021.0546875    18798.44726562]
 [  27180.83984375   74350.421875     78075.78125   ]
 [  19297.88476562   82161.140625      1204.53503418]]

print MW_SecondsubPos1 

[[  51850.9140625   106004.0078125    69536.5234375 ]
 [  33989.9375       11847.11425781   12255.80859375]
 [  12526.203125     58372.3046875    37641.34765625]
 ..., 
 [  98823.2734375    95837.1796875    18758.7734375 ]
 [  27047.19140625   74242.859375     78166.703125  ]
 [  19353.97851562   82375.8515625     1147.07556152]]

是的,它们的形状相同。

我的尝试,

import numpy as np

xs1,ys1,zs1 = zip(*MW_FirstsubPos1)
xs11,ys11,zs11 = zip(*MW_SecondsubPos1)

squared_dist1 = (xs11 - xs1)**2 + (ys11 - ys1)**2 + (zs11 - zs1)**2
dist1 = np.sqrt(squared_dist1)

print dist1

返回:

TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

我只想返回相同形状的一维数组。

* ---------------------更新--------------------- *

使用Sнаđошƒаӽ所说的,

Distance1 = []
for Fir1, Sec1 in zip(MW_FirstsubVel1, MW_SecondsubPos1):
    dist1 = 0
    for i in range(3):
        dist1 += (Fir1[i]-Sec1[i])**2
    Distance1.append(dist1**0.5)

但是在比较原始帖子中的一个元素的距离公式时,例如,

squared_dist1 = (xs11[0] - xs1[0])**2 + (ys11[0] - ys1[0])**2 + (zs11[0] - zs1[0])**2
dist1 = np.sqrt(squared_dist1)

print dist1

返回322.178309762

,而

result = []
for a, b in zip(MW_FirstsubVel1, MW_SecondsubPos1):
    dist = 0
    for i in range(3):
        dist += (a[i]-b[i])**2
    result.append(dist**0.5)

print result[0]

返回137163.203004

这里有什么问题?

3 个答案:

答案 0 :(得分:2)

您的解决方案对我来说很好。 更好的想法是在scipy包中使用线性代数模块,因为它可以与多维数据进行缩放。这是我的代码。

import scipy.linalg as LA

dist1 = LA.norm(MW_FirstsubPos1 - MW_SecondsubPos1, axis=1)

答案 1 :(得分:2)

看看这是否有效,假设aaabbb是具有x,y和z坐标的列表的正常python列表(或者您可以使用tolist转换为此类列表或类似的东西)。 result将拥有您正在寻找的1-D阵列。

编辑: aaabbb是列表的python列表。仅添加了用于打印输出的代码。

aaa = [[51618.7265625, 106197.7578125, 69647.6484375],
[33864.1953125, 11757.29882812, 11849.90332031],
[12750.09863281, 58954.91015625, 38067.0859375],
[99002.6640625, 96021.0546875, 18798.44726562],
[27180.83984375, 74350.421875, 78075.78125],
[19297.88476562, 82161.140625, 1204.53503418]]

bbb = [[51850.9140625, 106004.0078125, 69536.5234375],
[33989.9375, 11847.11425781, 12255.80859375],
[12526.203125, 58372.3046875, 37641.34765625],
[98823.2734375, 95837.1796875, 18758.7734375],
[27047.19140625, 74242.859375, 78166.703125],
[19353.97851562, 82375.8515625, 1147.07556152]]

result = []
for a, b in zip(aaa, bbb):
    dist = 0
    for i in range(3):
        dist += (a[i]-b[i])**2
    result.append(dist**0.5)

for elem in result:
    print(elem)

输出:

322.178309762234
434.32361222259755
755.5206249710258
259.9327309143388
194.16071591842936
229.23543894772612

答案 2 :(得分:1)

这是使用np.einsum -

的矢量化方法
diffs = MW_FirstsubPos1 - MW_SecondsubPos1
dists = np.sqrt(np.einsum('ij,ij->i',diffs,diffs))

示例运行 -

In [233]: MW_FirstsubPos1
Out[233]: 
array([[2, 0, 0],
       [8, 6, 1],
       [0, 2, 8],
       [7, 6, 3],
       [3, 1, 7]])

In [234]: MW_SecondsubPos1
Out[234]: 
array([[3, 4, 7],
       [0, 8, 4],
       [4, 7, 4],
       [2, 5, 6],
       [5, 0, 6]])

In [235]: diffs = MW_FirstsubPos1 - MW_SecondsubPos1

In [236]: np.sqrt(np.einsum('ij,ij->i',diffs,diffs))
Out[236]: array([ 8.1240384 ,  8.77496439,  7.54983444,  5.91607978,  2.44948974])