I have two numpy arrays A and B. Shape of A is (m,3) and shape of B is (n, 3).
Those matrix look like this:
A
#output
array([[ 9.227, -4.698, -95.607],
[ 10.294, -4.659, -94.606],
[ 11.184, -5.906, -94.675],
...,
[ 19.538, -91.572, -45.361],
[ 20.001, -92.655, -45.009],
[ 19.271, -92.726, -45.79 ]])
So it contains for each row the coordinates x,y,z of a 3D point. B follows the same format.
I have this function (np is numpy):
def compute_dist(point1, point2):
squared = (point1-point2)**2
return (np.sqrt(np.sum(squares)))
I want to compute a pairwise distance between A and B by using a vectorized function.
I try this:
v = np.vectorize(compute_dist)
v(A, B)
#output
matrix([[37.442, 42.693, 72.705],
[37.442, 42.693, 72.705],
[37.442, 42.693, 72.705],
...,
[37.442, 42.693, 72.705],
[37.442, 42.693, 72.705],
[37.442, 42.693, 72.705]])
I don't understand how to use vectorize even if I read the doc. How can I compute a matrix which contains pairwise distance between A and B? I know there is scipy.distance.cdist but I want to do it myself with np.vectorize.
I don't care about the format of the output (list, array, matrix ...). At the end I just want to find the minimal distance.