source: distools/psdistm.m @ 25

Last change on this file since 25 was 10, checked in by bduin, 14 years ago
File size: 2.3 KB
Line 
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
41function D = psdistm(A,B,sig)
42
43bisa = 0;
44if nargin < 2,
45  error ('Inputs not specified');
46elseif 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
54else
55  ;
56end
57
58isda = isdataset(A);
59isdb = isdataset(B);
60a    = +A;
61b    = +B;
62[ra,ca] = size(a);
63[rb,cb] = size(b);
64
65if ca ~= cb,
66  error ('The matrices should have the same number of columns.');
67end
68
69if any(sig) < 0 | sum(sig) ~= ca,
70  error('Signature vector SIG is invalid.');
71end
72
73J = [ones(1,sig(1))  -ones(1,sig(2))];
74D = - 2 .* a * diag(J) * b';
75D = D + ones(ra,1) * (J*(b'.*b'));
76D = D + (J * (a'.*a'))' * ones(1,rb);
77
78% Check numerical inaccuracy
79if bisa,
80  D = 0.5*(D+D');         % Make sure that distances are symmetric for D(A,A)
81end
82
83% Set object labels and feature labels
84if xor(isda, isdb),
85  prwarning(1,'One matrix is a dataset and the other not. ')
86end
87if 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
97end
98return
Note: See TracBrowser for help on using the repository browser.