Following the analysis of Stuart Layton, I've found a merely faster way to perform the same computation:

d = sqrt(bsxfun(@minus,bsxfun(@plus,sum(a.^2,1),sum(b.^2,1)'),2*(a'*b).'));

Using the same tool as Stuart Layton (modified to have the tic/toc outside the loop), I measure 4.3607ms for distance.m and 4.1252ms for mine. Does not change dramatically, ok.

11 Nov 2013

distance.m
A fully vectorized function that computes the Euclidean distance matrix between two sets of vectors.
Author: R. Bunschoten