I have the following arrays:
a = np.random.uniform(low=0, high=1, size=(5,3))
b = np.random.uniform(low=0, high=1, size=(2,3))
How can I concisely subtract each row from b
from each row in a
such that the result is of shape (5,2)
. Letting the result be c
, c[i,j]
is the euclidean distance between a[i,:]
and b[j,:]
.
I can get this behavior when there is only 1 column:
a = np.random.uniform(low=0, high=1, size=(5,1))
b = np.random.uniform(low=0, high=1, size=(2,1))
print((a-b.T).shape)