1 | %SIMDISTM Distance matrix between two data sets found from similarities |
---|
2 | % |
---|
3 | % D = SIMDISTM (A,B) |
---|
4 | % |
---|
5 | % INPUT |
---|
6 | % A NxK matrix or dataset |
---|
7 | % B MxK matrix or dataset (optional; default: B=A) |
---|
8 | % |
---|
9 | % OUTPUT |
---|
10 | % D NxM dissimilarity matrix or dataset; D in [0,1] |
---|
11 | % |
---|
12 | % DESCRIPTION |
---|
13 | % Computes the distance matrix D between two sets of vectors: A and B. |
---|
14 | % Distances between vectors X and Y are computed based on the similarity |
---|
15 | % formula: |
---|
16 | % SIM(X,Y) = 2 * (X'Y) / (||X||^2 + ||Y||^2) |
---|
17 | % D(X,Y) = (1 - SIM(X,Y)) |
---|
18 | % |
---|
19 | % If A and B are datasets, then D is a dataset as well with the labels defined |
---|
20 | % by the labels of A and the feature labels defined by the labels of B. If A is |
---|
21 | % not a dataset, but a matrix of doubles, then D is also a matrix of doubles. |
---|
22 | % |
---|
23 | % DEFAULT |
---|
24 | % B = A |
---|
25 | % |
---|
26 | % SEE ALSO |
---|
27 | % JACSIMDISTM, CORRDISTM, COSDISTM, LPDISTM, EUDISTM |
---|
28 | |
---|
29 | % Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com |
---|
30 | % Faculty EWI, Delft University of Technology and |
---|
31 | % School of Computer Science, University of Manchester |
---|
32 | |
---|
33 | |
---|
34 | function D = simdistm(A,B) |
---|
35 | |
---|
36 | bisa = nargin < 2; |
---|
37 | if bisa, |
---|
38 | B = A; |
---|
39 | end |
---|
40 | |
---|
41 | isda = isdataset(A); |
---|
42 | isdb = isdataset(B); |
---|
43 | a = +A; |
---|
44 | b = +B; |
---|
45 | |
---|
46 | [ra,ca] = size(a); |
---|
47 | [rb,cb] = size(b); |
---|
48 | |
---|
49 | if ca ~= cb, |
---|
50 | error ('Matrices should have equal numbers of columns'); |
---|
51 | end |
---|
52 | |
---|
53 | |
---|
54 | aa = sum(a.*a,2); |
---|
55 | bb = sum(b.*b,2)'; |
---|
56 | D = (2 * (a * b')) ./ (aa(:,ones(rb,1)) + bb(ones(ra,1),:)); |
---|
57 | D = 1 - D; |
---|
58 | |
---|
59 | % Check numerical inaccuracy |
---|
60 | D (find (D < eps)) = 0; % Make sure that distances are nonnegative |
---|
61 | if bisa, |
---|
62 | D = 0.5*(D+D'); % Make sure that distances are symmetric for D(A,A) |
---|
63 | end |
---|
64 | |
---|
65 | |
---|
66 | % Set object labels and feature labels |
---|
67 | if xor(isda, isdb), |
---|
68 | prwarning(1,'One matrix is a dataset and the other not. ') |
---|
69 | end |
---|
70 | if isda, |
---|
71 | if isdb, |
---|
72 | D = setdata(A,D,getlab(B)); |
---|
73 | else |
---|
74 | D = setdata(A,D); |
---|
75 | end |
---|
76 | D.name = 'Distance matrix'; |
---|
77 | if ~isempty(A.name) |
---|
78 | D.name = [D.name ' for ' A.name]; |
---|
79 | end |
---|
80 | end |
---|
81 | return |
---|