I have an n
by d
matrix U
and an m
by d
matrix V
, and I want to compute the pairwise distance between each row and U
and each row in V
. Essentially, I want a matrix M
such that M[i][j] == np.linalg.norm(U[i] - V[j]) ** 2
. I have to do this computation a lot, so I want to vectorize it. Right now, it's way too slow to just use a 2d for loop. Apparently, matlab has a way of doing this using pdist2
, but I'm not sure if there is a numpy equivalent.
Asked
Active
Viewed 1,548 times
4

Community
- 1
- 1

michaelsnowden
- 6,031
- 2
- 38
- 83
-
@Divakar That worked like a charm. Thank you! – michaelsnowden Apr 23 '17 at 20:31