如何计算到列表中点的距离? [英] How to calculate distance from points in lists?

查看:96
本文介绍了如何计算到列表中点的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组列表,A和O.它们都具有x,y z坐标上的点.我想计算A和B之间的点之间的距离.我使用了for循环,但它只给我一个结果.结果应该给我8个数字.我非常感谢有人可以看一看.这是我项目的最后一步.

I have two group of lists, A and O. Both of them have points from x,y z coordinate. I want to calculate the distance between points from A and B. I used a for loop, but it only give me one result. It should give me 8 numbers from the results. I'm very appreciate that someone can have a look. It's the last step in my project.

Ax = [-232.34, -233.1, -232.44, -233.02, -232.47, -232.17, -232.6, -232.29, -231.65] 
Ay = [-48.48, -49.48, -50.81, -51.42, -51.95, -52.25, -52.83, -53.63, -53.24] 
Az = [-260.77, -253.6, -250.25, -248.88, -248.06, -247.59, -245.82, -243.98, -243.76]
Ox = [-302.07, -302.13, -303.13, -302.69, -303.03, -302.55, -302.6, -302.46, -302.59] 
Oy = [-1.73, -3.37, -4.92, -4.85, -5.61, -5.2, -5.91, -6.41, -7.4] 
Oz = [-280.1, -273.02, -269.74, -268.32, -267.45, -267.22, -266.01, -264.79, -264.96]
distance = []
for xa in A1:
    for ya in A2:
        for za in A3:
            for x1 in o1:
                for y1 in o2:
                    for z1 in o3:
                        distance += distance
                        distance = (((xa-x1)**2)+((ya-y1)**2)+((za-z1)**2))**(1/2)  
print(distance)

推荐答案

其他人提供了针对您当前问题的修复程序.我还建议您开始使用numpy,并避免所有这些for循环. Numpy提供了使代码矢量化的方法,基本上可以将非常高效的C ++实现所需的所有循环都卸载了.例如,您可以使用以下矢量化实现替换整个嵌套的for循环对象:

Other people have provided fixes to your immediate problem. I would also recommend that you start using numpy and avoid all of those for loops. Numpy provides ways to vectorize your code, basically offload all of the looping that needs to be done to very efficient C++ implementations. For instance, you can replace your whole nested for-loop thing with the following vectorized implementation:

import numpy as np

# Convert your arrays to numpy arrays
Ax = np.asarray(Ax)
Ay = np.asarray(Ay)
Az = np.asarray(Az)
Ox = np.asarray(Ox)
Oy = np.asarray(Oy)
Oz = np.asarray(Oz)
# Find the distance in a single, vectorized operation
np.sqrt(np.sum(((Ax-Ox)**2, (Ay-Oy)**2, (Az-Oz)**2), axis=0))

这篇关于如何计算到列表中点的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆