简体   繁体   English

如何在没有 numpy 的情况下计算距离

[英]How to calculate distance without numpy

 myList =  [[0, 0, 0], #0
       [6.888437030500963, 5.159088058806049, -1.5885683833831], #1
       [2.0667720363602307, 5.384582486178219, -3.4898856343748133], #2
       [7.742743817055683, 1.4508370077567676,-3.957946551327696],#3
       [9.410384606156306, 9.613094711663472, -3.864209434979891],#4
       [5.047141494150383, 14.72917879480795, -1.4968295014732576],#5
       [0.05726832139919402,22.924103914172754, 8.158880019279806],#6
       [6.261613041330982, 30.96742292296441,4.361831405666459], #7
       [10.858248006533554, 38.94418868232428, 8.041510043975286],#8
       [10.30110231558782, 30.958212843691598, 6.724946753050958],#9
       [12.518841784463852,39.21843390844956, 16.057074108466132]]#10

import math

def distance (myList):

    dist = math.sqrt ((xa-xb)**2 + (ya-yb)**2 + (za-zb)**2)
    return dist

print("Distance:",(distance(myList)))

How can I calculate the distance of all that points but without NumPy?如果没有 NumPy,我如何计算所有这些点的距离? I understand how to do it with 2 but not with more than 2我知道如何用 2 但不能超过 2

import math

def distance (myList):
  distlist=[]
  for a in myList:
    for b in myList:
      dist = math.sqrt ((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)
      distlist.append(dist)
  return distlist

print("Distance:",(distance(myList)))

You have to append each result to a list you previously generated or you will store only the last value您必须将每个结果附加到您之前生成的列表中,否则您将只存储最后一个值

We can find the euclidian distance with the equation: d = sqrt((px1 - px2)^2 + (py1 - py2)^2 + (pz1 - pz2)^2)我们可以用等式找到欧几里得距离:d = sqrt((px1 - px2)^2 + (py1 - py2)^2 + (pz1 - pz2)^2)

Implementing in python:在python中实现:

import math

def dist(list):
  cummulativeDist = 0

  # iterate over sets of points
  for i in range(len(list) - 1):
    coordInit = list[i]
    coordFinal = list[i+1]

    # distance from one pt to the next
    dist = math.sqrt(((coordInit[0] - coordFinal[0]) ** 2) 
                    + ((coordInit[1] - coordFinal[2]) ** 2) 
                    + ((coordInit[2] - coordFinal[2]) ** 2)) 

    cummulativeDist += dist

  return cummulativeDist

print(f"Distance: {dist(myList)}")

This will take the 3 dimensional distance and from one point to the next and return the total distance traveled.这将采用 3 维距离和从一个点到下一个点并返回行进的总距离。

from math import sqrt def euclidean_distance(x, y): return sqrt(sum((px - py)**2 for px, py in zip(x,y))) euclidean_distance(x,y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM