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