Skip to content

Instantly share code, notes, and snippets.

@Killavus
Created November 3, 2015 12:31
Show Gist options
  • Save Killavus/6d6f7393080734ade070 to your computer and use it in GitHub Desktop.
Save Killavus/6d6f7393080734ade070 to your computer and use it in GitHub Desktop.
# M1 (N x A) -> M2 (N x B) -> DM (A x B)
def ComputeDistanceMatrix(M1, M2):
# For each r-th row: Sum(k = 1, N) M1(r, k) ** 2
SM1 = np.sum(M1 ** 2)
# For each r-th row: Sum(k = 1, N) M2(r, k) ** 2
SM2 = np.sum(M2 ** 2)
# The last term: 2 * (M1 x M2^T)
M1M2_2 = 2 * np.dot(M1, M2.T)
return np.sqrt(M1M2_2 + SM1 + SM2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment