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