1 | %EXPDISTM Exponential-type of Distance Matrix |
---|
2 | % |
---|
3 | % D = EXPDISTM (A,B,R) |
---|
4 | % or |
---|
5 | % D = EXPDISTM (A,B) |
---|
6 | % or |
---|
7 | % D = EXPDISTM (A,R) |
---|
8 | % or |
---|
9 | % D = EXPDISTM (A) |
---|
10 | % |
---|
11 | % INPUT |
---|
12 | % A NxK Matrix or dataset |
---|
13 | % B MxK Matrix or dataset (optional; default: B=A) |
---|
14 | % R Parameter to scale the Gaussian function |
---|
15 | % (optional; default: sqrt(N)*(max(max(A))- min(min(A)))) |
---|
16 | % |
---|
17 | % OUTPUT |
---|
18 | % D NxM Dissimilarity matrix or dataset |
---|
19 | % |
---|
20 | % DESCRIPTION |
---|
21 | % Computes the distance matrix D between two sets of vectors, A and B. |
---|
22 | % Given the vectors X and Y, distances are computed as: |
---|
23 | % D(X,Y) = 1 - exp (-(X-Y)'(X-Y)/R^2) |
---|
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 | % SEE ALSO |
---|
30 | % SIMDISTM, JACSIMDISTM, COSDISTM, CORRDISTM, LPDISTM, EUDISTM |
---|
31 | |
---|
32 | % Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com |
---|
33 | % Faculty EWI, Delft University of Technology and |
---|
34 | % School of Computer Science, University of Manchester |
---|
35 | |
---|
36 | |
---|
37 | function [D,r] = expdistm(A,B,r) |
---|
38 | [ra,ca] = size(A); |
---|
39 | |
---|
40 | bisa = 0; |
---|
41 | if nargin < 2, |
---|
42 | bisa = 1; |
---|
43 | r = (max(max(+A)) - min(min(+A)))*sqrt(ra); |
---|
44 | B = A; |
---|
45 | [rb,cb] = size(B); |
---|
46 | else |
---|
47 | [rb,cb] = size(B); |
---|
48 | |
---|
49 | if nargin < 3, |
---|
50 | if max (rb,cb) == 1, |
---|
51 | bisa = 1; |
---|
52 | r = B; |
---|
53 | B = A; |
---|
54 | [rb,cb] = size(B); |
---|
55 | else |
---|
56 | r = (max(max(+A)) - min(min(+A)))*sqrt(ra); |
---|
57 | end |
---|
58 | end |
---|
59 | end |
---|
60 | |
---|
61 | |
---|
62 | if ~bisa & ca ~= cb, |
---|
63 | error ('The matrices should have the same number of columns.'); |
---|
64 | end |
---|
65 | |
---|
66 | isda = isa(A,'dataset'); |
---|
67 | isdb = isa(B,'dataset'); |
---|
68 | a = +A; |
---|
69 | b = +B; |
---|
70 | |
---|
71 | |
---|
72 | %d = zeros(ra,rb); |
---|
73 | %d = sum ((abs (repmat (permute(a,[1 3 2]), [1 rb 1]) - ... |
---|
74 | % repmat (permute(b,[3 1 2]), [ra 1 1]))).^2,3); |
---|
75 | |
---|
76 | D = 1 - exp (-distm(a,b)/r^2); |
---|
77 | D(find(D < eps)) = 0; |
---|
78 | |
---|
79 | |
---|
80 | % Set object labels and feature labels |
---|
81 | if xor(isda, isdb), |
---|
82 | prwarning(1,'One matrix is a dataset and the other not. ') |
---|
83 | end |
---|
84 | if isda, |
---|
85 | if isdb, |
---|
86 | D = setdata(A,D,getlab(B)); |
---|
87 | else |
---|
88 | D = setdata(A,D); |
---|
89 | end |
---|
90 | D.name = 'Distance matrix'; |
---|
91 | if ~isempty(A.name) |
---|
92 | D.name = [D.name ' for ' A.name]; |
---|
93 | end |
---|
94 | end |
---|
95 | return |
---|