Suppose I have the following 2 arrays:
import numpy as np
a=np.asarray([[1,2,4],
[3,1,2]])
b=np.asarray([[2,1,1],
[3,2,3],
[4,1,2],
[2,2,1],])
For every row a_row in a, I would like to get the sum of squared difference between a_row and every row in b. The resulted array would be a 2 by 4 array. The expected result would be the following:
array([[ 11., 5., 14., 10.],
[ 2., 2., 1., 3.]])
I've already implemented a solution using loop:
c=np.zeros((2,4))
for e in range(a.shape[0]):
c[e,:] = np.sum(np.square(b-a[e,:]),axis=1)
print c
What I need is a fully vectorized solution, i.e. no loop is required.