Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

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 + (za-zb)^2)

What's the best way to do this with NumPy, or with Python in general? I have:

a = numpy.array((xa ,ya, za)


b = numpy.array((xb,

yb, zb))

1 Answer

0 votes
by (106k points)

To calculate  Euclidean distance with NumPy you can use numpy.linalg.norm:

numpy.linalg.norm(x, ord=None, axis=None, keepdims=False):-

It is a function which is able to return one of eight different matrix norms, or one of an infinite number of vector norms, depending on the value of the ord parameter.

You can use the following piece of code to calculate the distance:-

import numpy as np

from numpy import linalg as LA

a = (1, 2, 3)

b = (4, 5, 6)

dist = numpy.linalg.norm(a-b)

If you want to learn Python, visit this Python tutorial and Python course.

Related questions

0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories