I have a matrix A of shape m x n and another smaller matrix B of shape k x n. I want to calculate the euclidean distance between the rows of A and B, generating a matrix C of shape m x k. I already have a function dist(row1, row2)
. This is trivial using loops, but is there a vectorized way to do this in NumPy?
I believe what I want can be translated to a custom matrix multiplication-like operation (if I transpose B), and this question seems to head in the same direction, but the best answer there rearrange the operations in order to achieve vectorization (I want to use my separate function dist(row1, row2)
). The second answer uses a separate function, but it also use loops.