annotate toolboxes/FullBNT-1.0.7/KPMtools/sqdist.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function m = sqdist(p, q, A)
wolffd@0 2 % SQDIST Squared Euclidean or Mahalanobis distance.
wolffd@0 3 % SQDIST(p,q) returns m(i,j) = (p(:,i) - q(:,j))'*(p(:,i) - q(:,j)).
wolffd@0 4 % SQDIST(p,q,A) returns m(i,j) = (p(:,i) - q(:,j))'*A*(p(:,i) - q(:,j)).
wolffd@0 5
wolffd@0 6 % From Tom Minka's lightspeed toolbox
wolffd@0 7
wolffd@0 8 [d, pn] = size(p);
wolffd@0 9 [d, qn] = size(q);
wolffd@0 10
wolffd@0 11 if nargin == 2
wolffd@0 12
wolffd@0 13 pmag = sum(p .* p, 1);
wolffd@0 14 qmag = sum(q .* q, 1);
wolffd@0 15 m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*q;
wolffd@0 16 %m = ones(pn,1)*qmag + pmag'*ones(1,qn) - 2*p'*q;
wolffd@0 17
wolffd@0 18 else
wolffd@0 19
wolffd@0 20 if isempty(A) | isempty(p)
wolffd@0 21 error('sqdist: empty matrices');
wolffd@0 22 end
wolffd@0 23 Ap = A*p;
wolffd@0 24 Aq = A*q;
wolffd@0 25 pmag = sum(p .* Ap, 1);
wolffd@0 26 qmag = sum(q .* Aq, 1);
wolffd@0 27 m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*Aq;
wolffd@0 28
wolffd@0 29 end