diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toolboxes/FullBNT-1.0.7/KPMtools/sqdist.m	Tue Feb 10 15:05:51 2015 +0000
@@ -0,0 +1,29 @@
+function m = sqdist(p, q, A)
+% SQDIST      Squared Euclidean or Mahalanobis distance.
+% SQDIST(p,q)   returns m(i,j) = (p(:,i) - q(:,j))'*(p(:,i) - q(:,j)).
+% SQDIST(p,q,A) returns m(i,j) = (p(:,i) - q(:,j))'*A*(p(:,i) - q(:,j)).
+
+%  From Tom Minka's lightspeed toolbox
+
+[d, pn] = size(p);
+[d, qn] = size(q);
+
+if nargin == 2
+  
+  pmag = sum(p .* p, 1);
+  qmag = sum(q .* q, 1);
+  m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*q;
+  %m = ones(pn,1)*qmag + pmag'*ones(1,qn) - 2*p'*q;
+  
+else
+
+  if isempty(A) | isempty(p)
+    error('sqdist: empty matrices');
+  end
+  Ap = A*p;
+  Aq = A*q;
+  pmag = sum(p .* Ap, 1);
+  qmag = sum(q .* Aq, 1);
+  m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*Aq;
+  
+end