annotate toolboxes/FullBNT-1.0.7/netlab3.3/dist2.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function n2 = dist2(x, c)
Daniel@0 2 %DIST2 Calculates squared distance between two sets of points.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % D = DIST2(X, C) takes two matrices of vectors and calculates the
Daniel@0 6 % squared Euclidean distance between them. Both matrices must be of
Daniel@0 7 % the same column dimension. If X has M rows and N columns, and C has
Daniel@0 8 % L rows and N columns, then the result has M rows and L columns. The
Daniel@0 9 % I, Jth entry is the squared distance from the Ith row of X to the
Daniel@0 10 % Jth row of C.
Daniel@0 11 %
Daniel@0 12 % See also
Daniel@0 13 % GMMACTIV, KMEANS, RBFFWD
Daniel@0 14 %
Daniel@0 15
Daniel@0 16 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 17
Daniel@0 18 [ndata, dimx] = size(x);
Daniel@0 19 [ncentres, dimc] = size(c);
Daniel@0 20 if dimx ~= dimc
Daniel@0 21 error('Data dimension does not match dimension of centres')
Daniel@0 22 end
Daniel@0 23
Daniel@0 24 n2 = (ones(ncentres, 1) * sum((x.^2)', 1))' + ...
Daniel@0 25 ones(ndata, 1) * sum((c.^2)',1) - ...
Daniel@0 26 2.*(x*(c'));
Daniel@0 27
Daniel@0 28 % Rounding errors occasionally cause negative entries in n2
Daniel@0 29 if any(any(n2<0))
Daniel@0 30 n2(n2<0) = 0;
Daniel@0 31 end