pdist2 equivalent in MATLAB version 7

Here is vectorized implementation for computing the euclidean distance that is much faster than what you have (even significantly faster than PDIST2 on my machine): D = sqrt( bsxfun(@plus,sum(A.^2,2),sum(B.^2,2)’) – 2*(A*B’) ); It is based on the fact that: ||u-v||^2 = ||u||^2 + ||v||^2 – 2*u.v Consider below a crude comparison between the two methods: … Read more

Minimum Euclidean distance between points in two different Numpy arrays, not within

(Months later) scipy.spatial.distance.cdist( X, Y ) gives all pairs of distances, for X and Y 2 dim, 3 dim … It also does 22 different norms, detailed here . # cdist example: (nx,dim) (ny,dim) -> (nx,ny) from __future__ import division import sys import numpy as np from scipy.spatial.distance import cdist #……………………………………………………………………. dim = 10 nx … Read more

Efficiently compute pairwise squared Euclidean distance in Matlab

The usually given answer here is based on bsxfun (cf. e.g. [1]). My proposed approach is based on matrix multiplication and turns out to be much faster than any comparable algorithm I could find: helpA = zeros(numA,3*d); helpB = zeros(numB,3*d); for idx = 1:d helpA(:,3*idx-2:3*idx) = [ones(numA,1), -2*A(:,idx), A(:,idx).^2 ]; helpB(:,3*idx-2:3*idx) = [B(:,idx).^2 , B(:,idx), … Read more