1 | %PE_DISTM Square Pseudo-Euclidean (PE) Distance Between Two Datasets |
---|
2 | % |
---|
3 | % D = PE_DISTM(A) |
---|
4 | % OR |
---|
5 | % D = PE_DISTM(A,B) |
---|
6 | % |
---|
7 | % INPUT |
---|
8 | % A PE dataset of size NxK |
---|
9 | % B PE dataset of size MxK (default A = B) |
---|
10 | % |
---|
11 | % OUTPUT |
---|
12 | % D NxM dissimilarity matrix or dataset |
---|
13 | % |
---|
14 | % DESCRIPTION |
---|
15 | % Computation of the square pseudo-Euclidean distance matrix D between two sets |
---|
16 | % of vectors, A and B. The pseudo-Euclidean distance with the signature SIG |
---|
17 | % (e.g. SIG = [10 5]) between vectors X and Y is computed as an indefinite |
---|
18 | % 'Euclidean' distance: |
---|
19 | % D(X,Y) = (X-Y)'*J*(X-Y), |
---|
20 | % where J is a diagonal matrix with 1's, followed by -1's. |
---|
21 | % J = diag ([ONES(SIG(1),1); -ONES(SIG(2),1)]); |
---|
22 | % |
---|
23 | % In a PE dataset the signature is stored in the user field, see |
---|
24 | % SETSIG. This signature is derived from A. It is not stored in D as D |
---|
25 | % does not contain vectors in a PE space. |
---|
26 | % |
---|
27 | % D is a dataset with the labels defined by the labels of A and feature labels |
---|
28 | % defined by the labels of B. |
---|
29 | % |
---|
30 | % REMARKS |
---|
31 | % Note that square pseudo-Euclidean distances can be negative. |
---|
32 | % |
---|
33 | % SEE ALSO |
---|
34 | % DATASET, SETSIG, DISTM |
---|
35 | |
---|
36 | % Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com |
---|
37 | % Faculty EWI, Delft University of Technology and |
---|
38 | % School of Computer Science, University of Manchester |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | function D = pe_distm(A,B) |
---|
43 | |
---|
44 | isdataset(A); |
---|
45 | bisa = 0; |
---|
46 | if nargin < 2, B = A; bisa = 1; end |
---|
47 | sig = getsig(A); |
---|
48 | |
---|
49 | if ~isdataset(B), B = dataset(B,1); end |
---|
50 | |
---|
51 | a = +A; |
---|
52 | b = +B; |
---|
53 | [ra,ca] = size(a); |
---|
54 | [rb,cb] = size(b); |
---|
55 | |
---|
56 | if ca ~= cb, |
---|
57 | error ('The datasets should have the same number of features.'); |
---|
58 | end |
---|
59 | |
---|
60 | if any(sig) < 0 | sum(sig) ~= ca, |
---|
61 | error('Signature vector SIG is invalid: its sum should equal feature size'); |
---|
62 | end |
---|
63 | |
---|
64 | J = [ones(1,sig(1)) -ones(1,sig(2))]; |
---|
65 | D = - 2 .* a * diag(J) * b'; |
---|
66 | D = D + ones(ra,1) * (J*(b'.*b')); |
---|
67 | D = D + (J * (a'.*a'))' * ones(1,rb); |
---|
68 | |
---|
69 | if bisa |
---|
70 | D = 0.5*(D+D'); % Make sure that distances are symmetric for D(A,A) |
---|
71 | end |
---|
72 | |
---|
73 | % Set object labels and feature labels |
---|
74 | D = setdata(A,D,getlab(B)); |
---|
75 | D.name = 'Square Pseudo-Euclidean distance matrix'; |
---|
76 | if ~isempty(A.name) |
---|
77 | D.name = [D.name ' for ' A.name]; |
---|
78 | end |
---|
79 | |
---|
80 | return |
---|