0

I have a square matrix of like this:

[[ 1.,  2.,  3.],
 [ 4.,  5.,  6.],
 [ 7.,  8.,  9.]]

and want a matrix like this:

[[0.,   27., 108.],
 [27.,   0.,  27.],
 [108., 27.,   0.]]

which contains the (here: squared euclidean) distance between each row vector and the other rows in the columns.

What is the most efficient way of creating such a matrix? Is there a way to do this with a "clever" Numpy operation, perhaps without explicit iteration?

user1603472
  • 1,408
  • 15
  • 24
  • 1
    This is more of a programming question; try stackoverflow. – Kontorus Jun 20 '16 at 19:14
  • I think it's a bit of both. There is also some statistical theory involved. Surely this matrix has a name, and there are ways to exploit the symmetry, etc. – user1603472 Jun 20 '16 at 19:16
  • I think this is answered [here](http://stackoverflow.com/a/22721540/2336654). – piRSquared Jun 20 '16 at 20:07
  • 1
    There's [`scipy.spatial.distance.pdist(x, metric='sqeuclidean')`](http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html), but I don't think it does anything clever. – user2357112 Jun 20 '16 at 20:08
  • 2
    You can use `squareform` on top of `pdist` to create a 2D array : `squareform(pdist(A,'sqeuclidean'))`. – Divakar Jun 20 '16 at 20:09

0 Answers0