[Numpy-discussion] Distance Formula on an Array

Damian Eads eads@soe.ucsc....
Tue May 12 20:44:56 CDT 2009

Hi Ian,

Sorry for responding so late. I've been traveling and I'm just
catching up on my e-mail now. This is easily accomplished with the
cdist function, which computes the pairwise distances between two sets
of vectors. In your case, one of the sets contains only a single

In [6]: scipy.spatial.distance.cdist([[0,4,0]],[[0,0,0],[0,1,0],[0,0,3]])
Out[6]: array([[ 4.,  3.,  5.]])

I hope this helps.



On Sat, Apr 25, 2009 at 11:50 AM, 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)
> Ideas?
> Thanks,
> Ian
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

Damian Eads                             Ph.D. Candidate
Jack Baskin School of Engineering, UCSC        E2-489
1156 High Street                 Machine Learning Lab
Santa Cruz, CA 95064    http://www.soe.ucsc.edu/~eads

More information about the Numpy-discussion mailing list