This function compute a matrix of square of Euclidean or Mahalanobis distances between vectors in the inputs. Column vectors are assumed in this code.
The code is fully optimized by vectorization. When the input is in the scale of 4000x4000, this function is ten times faster than the built-in function pdist.
When two matrices A and B are inputed, this function computes the square Euclidean distances between them. If an extra positive definite matrix M is inputed, it computes Mahalanobis distances.
If only one matrix A is inputed, it computes square Euclidean distances between vectors in A. In this case, it is equivalent to the square of pdist function in matlab statistics toolbox but much much faster(10~100 times).
Sample code:
d=1000;n1=5000;n2=6000;
A=rand(d,n1);B=rand(d,n2);
M=rand(d,d);M=M*M'+eye(d);
D1=sqdistance(A,B);
D2=sqdistance(A);
D3=sqdistance(A,B,M);
Detail explanation can be found in following blog post:
http://statinfer.wordpress.com/2011/11/14/efficient-matlab-i-pairwise-distances/ |