Say, that fig1 and fig2 are numpy 2d arrays of shape (n1, 2) and (n2, 2), which basically are lists of coordinates. What I'm tying to do is to count a mean Hausdorff distance between two figures presented as sets of dots. Here is what I've came up with:
@jit(nopython=True, fastmath=True)
def dist(fig1, fig2):
n1 = fig1.shape[0]
n2 = fig2.shape[0]
dist_matrix = np.zeros((n1, n2))
min1 = np.full(n1, np.inf)
min2 = np.full(n2, np.inf)
for i in range(n1):
for j in range(n2):
dist_matrix[i, j] = np.sqrt((fig1[i, 0] - fig2[j, 0])**2 + (fig1[i, 1] - fig2[j, 1])**2)
min1[i] = np.minimum(min1[i], dist_matrix[i, j])
min2[j] = np.minimum(min2[j], dist_matrix[i, j])
d1 = np.mean(min1)
d2 = np.mean(min2)
h_dist = np.maximum(d1, d2)
return h_dist
That works perfectly, but I'd like to avoid these nested for's, hopefully via vectorization. Can somebody suggest any possible variants?
If needed I can give some generators of figures.