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