title: How to calculate the euclidean distance between two matrices using only matrix operations in numpy python (no for loops)?
body: <p>I am trying to calculate the euclidean distance between two matrices using only matrix operations in numpy python, but without using any for loops.</p>
  <p>If I needed to calculate this for only two single vectors it would be trivial since I would just use the formula for euclidean distance:</p>
  <blockquote>
  <p>D(x, y) = ∥y – x∥ = √ ( xT x + yT y – 2 xT y )</p>
  </blockquote>
  <p>and converting that into numpy would be as simple as doing something like this</p>
  <p><code>a@a.T - 2*a@b.T + b@b.T</code></p>
  <p>however, doing it for two matrices of size PxQ and RxQ is proving to be tricky without the use of for loops.</p>
  <p>To give a better example of what I am trying to do if I had this matrix called A:</p>
  <pre><code>[[ 4  9  9]
   [11  8  1]
   [ 2  6  4]
   [ 4  7 11]
   [ 6  7  9]]
  </code></pre>
  <p>and a matrix called B:</p>
  <pre><code>[[ 5  8  8]
   [10  5  1]
   [ 6  6  9]
   [ 2  1  2]
   [ 9  1  3]
   [ 6  1  6]
   [ 9 10  3]
   [10  4  8]]
  </code></pre>
  <p>Then I would want to be able to calculate the resultant matrix C:</p>
  <pre><code>[[ 1.73205081 10.77032961  3.60555128 10.81665383 11.18033989  8.77496439
     7.87400787  7.87400787]
   [ 9.21954446  3.16227766  9.64365076 11.44552314  7.54983444  9.94987437
     3.46410162  8.1240384 ]
   [ 5.38516481  8.60232527  6.40312424  5.38516481  8.66025404  6.70820393
     8.1240384   9.16515139]
   [ 3.31662479 11.83215957  3.         11.         11.18033989  8.06225775
     9.89949494  7.34846923]
   [ 1.73205081  9.16515139  1.         10.04987562  9.          6.70820393
     7.34846923  5.09901951]]
  </code></pre>
  <p>I believe the top solution on stack overflow for this thread <a href="https://stackoverflow.com/questions/19360047/how-to-build-a-distance-matrix-without-a-loop-vectorization">solution</a> is doing exactly that, but it is in matlab and I am having trouble converting it into a Python numpy solution.</p>