annotate toolboxes/FullBNT-1.0.7/KPMtools/sqdist.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 m = sqdist(p, q, A)
Daniel@0 2 % SQDIST Squared Euclidean or Mahalanobis distance.
Daniel@0 3 % SQDIST(p,q) returns m(i,j) = (p(:,i) - q(:,j))'*(p(:,i) - q(:,j)).
Daniel@0 4 % SQDIST(p,q,A) returns m(i,j) = (p(:,i) - q(:,j))'*A*(p(:,i) - q(:,j)).
Daniel@0 5
Daniel@0 6 % From Tom Minka's lightspeed toolbox
Daniel@0 7
Daniel@0 8 [d, pn] = size(p);
Daniel@0 9 [d, qn] = size(q);
Daniel@0 10
Daniel@0 11 if nargin == 2
Daniel@0 12
Daniel@0 13 pmag = sum(p .* p, 1);
Daniel@0 14 qmag = sum(q .* q, 1);
Daniel@0 15 m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*q;
Daniel@0 16 %m = ones(pn,1)*qmag + pmag'*ones(1,qn) - 2*p'*q;
Daniel@0 17
Daniel@0 18 else
Daniel@0 19
Daniel@0 20 if isempty(A) | isempty(p)
Daniel@0 21 error('sqdist: empty matrices');
Daniel@0 22 end
Daniel@0 23 Ap = A*p;
Daniel@0 24 Aq = A*q;
Daniel@0 25 pmag = sum(p .* Ap, 1);
Daniel@0 26 qmag = sum(q .* Aq, 1);
Daniel@0 27 m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*Aq;
Daniel@0 28
Daniel@0 29 end