source: distools/corrdistm.m @ 13

Last change on this file since 13 was 10, checked in by bduin, 14 years ago
File size: 1.9 KB
Line 
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
36function D = corrdistm (A,B)
37bisa = nargin < 2;
38if bisa,
39  B = A;
40end
41
42isda = isdataset(A);
43isdb = isdataset(B);
44a = +A;
45b = +B;
46
47[ra,ca] = size(a);
48[rb,cb] = size(b);
49
50if ca ~= cb,
51  error ('Matrices should have equal numbers of columns.');
52end
53
54ma  = mean(a');
55a   = a - ma' * ones(1,ca);
56mb  = mean(b');
57b   = b - mb' * ones(1,cb);
58
59aa = sum(a.*a,2);
60bb = sum(b.*b,2)';
61D  = (a*b') ./ sqrt(repmat(aa,1,rb) .* repmat(bb,ra,1));
62D  = 0.5 * (1 - D);
63
64% Check numerical inaccuracy
65D (find (D < eps)) = 0;   % Make sure that distances are nonnegative
66if bisa,
67  D = 0.5*(D+D');         % Make sure that distances are symmetric for D(A,A)
68end
69
70% Set object labels and feature labels
71if xor(isda, isdb),
72  prwarning(1,'One matrix is a dataset and the other not. ')
73end
74if 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
84end
85return
Note: See TracBrowser for help on using the repository browser.