1 | %PSDISTM Square Pseudo-Euclidean Distance Between Two Datasets |
---|
2 | % |
---|
3 | % D = PSDISTM(A,SIG) |
---|
4 | % OR |
---|
5 | % D = PSDISTM(A,B,SIG) |
---|
6 | % |
---|
7 | % INPUT |
---|
8 | % A NxK Matrix or dataset |
---|
9 | % B MxK Matrix or dataset |
---|
10 | % SIG 2x1 vector being the signature of the pseudo-Euclidean space; |
---|
11 | % SIG(1)+SIG(2)=K |
---|
12 | % |
---|
13 | % OUTPUT |
---|
14 | % D NxM dissimilarity matrix or dataset |
---|
15 | % |
---|
16 | % DESCRIPTION |
---|
17 | % Computation of the square pseudo-Euclidean distance matrix D between two sets |
---|
18 | % of vectors, A and B. The pseudo-Euclidean distance with the signature SIG |
---|
19 | % (e.g. SIG = [10 5]) between vectors X and Y is comuted as an indefinite |
---|
20 | % 'Euclidean' distance: |
---|
21 | % D(X,Y) = (X-Y)'*J*(X-Y), |
---|
22 | % where J is a diagonal matrix with 1, followed by -1. |
---|
23 | % J = diag ([ONES(SIG(1),1); -ONES(sig(2),1)]); |
---|
24 | % |
---|
25 | % If A and B are datasets, then D is a dataset as well with the labels defined |
---|
26 | % by the labels of A and the feature labels defined by the labels of B. If A is |
---|
27 | % not a dataset, but a matrix of doubles, then D is also a matrix of doubles. |
---|
28 | % |
---|
29 | % REMARKS |
---|
30 | % Note that square pseudo-Euclidean distances can be negative. |
---|
31 | % |
---|
32 | % SEE ALSO |
---|
33 | % DISTM |
---|
34 | |
---|
35 | % Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com |
---|
36 | % Faculty EWI, Delft University of Technology and |
---|
37 | % School of Computer Science, University of Manchester |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | function D = psdistm(A,B,sig) |
---|
42 | |
---|
43 | bisa = 0; |
---|
44 | if nargin < 2, |
---|
45 | error ('Inputs not specified'); |
---|
46 | elseif nargin < 3 |
---|
47 | if max (size(B)) == 2 & min(size(B)) == 1, |
---|
48 | sig = B; |
---|
49 | B = A; |
---|
50 | bisa = 1; |
---|
51 | else |
---|
52 | error('Signature vector SIG expected.'); |
---|
53 | end |
---|
54 | else |
---|
55 | ; |
---|
56 | end |
---|
57 | |
---|
58 | isda = isdataset(A); |
---|
59 | isdb = isdataset(B); |
---|
60 | a = +A; |
---|
61 | b = +B; |
---|
62 | [ra,ca] = size(a); |
---|
63 | [rb,cb] = size(b); |
---|
64 | |
---|
65 | if ca ~= cb, |
---|
66 | error ('The matrices should have the same number of columns.'); |
---|
67 | end |
---|
68 | |
---|
69 | if any(sig) < 0 | sum(sig) ~= ca, |
---|
70 | error('Signature vector SIG is invalid.'); |
---|
71 | end |
---|
72 | |
---|
73 | J = [ones(1,sig(1)) -ones(1,sig(2))]; |
---|
74 | D = - 2 .* a * diag(J) * b'; |
---|
75 | D = D + ones(ra,1) * (J*(b'.*b')); |
---|
76 | D = D + (J * (a'.*a'))' * ones(1,rb); |
---|
77 | |
---|
78 | % Check numerical inaccuracy |
---|
79 | if bisa, |
---|
80 | D = 0.5*(D+D'); % Make sure that distances are symmetric for D(A,A) |
---|
81 | end |
---|
82 | |
---|
83 | % Set object labels and feature labels |
---|
84 | if xor(isda, isdb), |
---|
85 | prwarning(1,'One matrix is a dataset and the other not. ') |
---|
86 | end |
---|
87 | if isda, |
---|
88 | if isdb, |
---|
89 | D = setdata(A,D,getlab(B)); |
---|
90 | else |
---|
91 | D = setdata(A,D); |
---|
92 | end |
---|
93 | D.name = 'Square Pseudo-Euclidean distance matrix'; |
---|
94 | if ~isempty(A.name) |
---|
95 | D.name = [D.name ' for ' A.name]; |
---|
96 | end |
---|
97 | end |
---|
98 | return |
---|