wolffd@0: function n2 = dist2(x, c) wolffd@0: %DIST2 Calculates squared distance between two sets of points. wolffd@0: % wolffd@0: % Description wolffd@0: % D = DIST2(X, C) takes two matrices of vectors and calculates the wolffd@0: % squared Euclidean distance between them. Both matrices must be of wolffd@0: % the same column dimension. If X has M rows and N columns, and C has wolffd@0: % L rows and N columns, then the result has M rows and L columns. The wolffd@0: % I, Jth entry is the squared distance from the Ith row of X to the wolffd@0: % Jth row of C. wolffd@0: % wolffd@0: % See also wolffd@0: % GMMACTIV, KMEANS, RBFFWD wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: [ndata, dimx] = size(x); wolffd@0: [ncentres, dimc] = size(c); wolffd@0: if dimx ~= dimc wolffd@0: error('Data dimension does not match dimension of centres') wolffd@0: end wolffd@0: wolffd@0: n2 = (ones(ncentres, 1) * sum((x.^2)', 1))' + ... wolffd@0: ones(ndata, 1) * sum((c.^2)',1) - ... wolffd@0: 2.*(x*(c')); wolffd@0: wolffd@0: % Rounding errors occasionally cause negative entries in n2 wolffd@0: if any(any(n2<0)) wolffd@0: n2(n2<0) = 0; wolffd@0: end