[Numpy-discussion] Distance Formula on an Array

Charles R Harris charlesr.harris@gmail....
Sat Apr 25 14:57:13 CDT 2009


On Sat, Apr 25, 2009 at 12:50 PM, Ian Mallett <geometrian@gmail.com> wrote:

> Hi,
>
> I have an array sized n*3.  Each three-component is a 3D position.  Given
> another 3D position, how is the distance between it and every
> three-component in the array found with NumPy?
>
> So, for example, if the array is:
> [[0,0,0],[0,1,0],[0,0,3]]
> And the position is:
> [0,4,0]
> I need this array out:
> [4,3,5]
> (Just a simple Pythagorean Distance Formula)
>

In [3]: vec = array([[0,0,0],[0,1,0],[0,0,3]])

In [4]: pos = array([0,4,0])

In [5]: sqrt(((vec - pos)**2).sum(1))
Out[5]: array([ 4.,  3.,  5.])

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090425/bff336ce/attachment.html 


More information about the Numpy-discussion mailing list