title: numpy - Computing "element-wise" difference between two arrays along first axis
body: <p>Suppose I have two arrays <code>A</code> and <code>B</code> with dimensions <code>(n1,m1,m2)</code> and <code>(n2,m1,m2)</code>, respectively. I want to compute the matrix <code>C</code> with dimensions <code>(n1,n2)</code> such that <code>C[i,j] = sum((A[i,:,:] - B[j,:,:])^2)</code>. Here is what I have so far:</p>
  
  <pre><code>import numpy as np
  A = np.array(range(1,13)).reshape(3,2,2)
  B = np.array(range(1,9)).reshape(2,2,2)
  C = np.zeros(shape=(A.shape[0], B.shape[0]) )
  for i in range(A.shape[0]):
      for j in range(B.shape[0]):
          C[i,j] = np.sum(np.square(A[i,:,:] - B[j,:,:]))
  C
  </code></pre>
  
  <p>What is the most efficient way to do this? In R I would use a vectorized approach, such as <code>outer</code>. Is there a similar method for Python?</p>
  
  <p>Thanks.</p>