How can the Euclidean distance be calculated with NumPy?

后端 未结 22 1498
春和景丽
春和景丽 2020-11-22 02:29

I have two points in 3D:

(xa, ya, za)
(xb, yb, zb)

And I want to calculate the distance:

dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (         


        
22条回答
  •  时光说笑
    2020-11-22 02:41

    Since Python 3.8

    Since Python 3.8 the math module includes the function math.dist().
    See here https://docs.python.org/3.8/library/math.html#math.dist.

    math.dist(p1, p2)
    Return the Euclidean distance between two points p1 and p2, each given as a sequence (or iterable) of coordinates.

    import math
    print( math.dist( (0,0),   (1,1)   )) # sqrt(2) -> 1.4142
    print( math.dist( (0,0,0), (1,1,1) )) # sqrt(3) -> 1.7321
    

提交回复
热议问题