source: distools/rankdistm.m @ 100

Last change on this file since 100 was 10, checked in by bduin, 14 years ago
File size: 3.5 KB
RevLine 
[10]1%RANKDISTM  Distance matrix between two data sets based on ranking
2%
3%     D = RANKDISTM (A,B,P)
4%       or
5%     D = RANKDISTM (A,B)
6%       or
7%     D = RANKDISTM (A,P)
8%       or
9%     D = RANKDISTM (A)
10%
11% INPUT
12%   A   NxK Matrix or dataset
13%   B   MxK Matrix or dataset
14%   P   Parameter:
15%       Integer - 1 .. K or 'MIN', 'MAX', 'MEDIAN' (optional; default: 'MEDIAN')
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 using the ranked
23% distance as:
24%   D(X,Y) = P-th value of (sort {|X_1 - Y_1|, |X_2 - Y_2|,..,|X_K - Y_K|})
25%
26% For instance, for P = 1, the ranked distance becomes the minimum value of
27% the differences |X_i - Y_i|, or for P = K, the infinty norm.
28%
29% If A and B are datasets, then D is a dataset as well with the labels defined
30% by the labels of A and the feature labels defined by the labels of B. If A is
31% not a dataset, but a matrix of doubles, then D is also a matrix of doubles.
32%
33% DEFAULT
34% P = 'MEDIAN'
35%
36% SEE ALSO
37% LPDISTM, EUDISTM, SIMDISTM, JACSIMDISTM, CORRDISTM, COSDISTM
38
39% Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com
40% Faculty EWI, Delft University of Technology and
41% School of Computer Science, University of Manchester
42
43
44function D = rankdistm (A,B,kk)
45bisa = 0;
46if nargin == 3,
47  k = whichk(kk,ca);
48elseif nargin < 2,
49  k = 0;                  % median
50  B = A;
51  bisa = 1;
52else
53  k = whichk(B,ca);
54  B = A;
55  bisa = 1;
56end
57
58isda = isdataset(A);
59isdb = isdataset(B);
60a    = +A;
61b    = +B;
62
63[ra,ca] = size(a);
64[rb,cb] = size(b);
65
66if ca ~= cb,
67  error ('The matrices should have the same number of columns.');
68end
69
70
71D = zeros(ra,rb);
72switch k,
73  case 0,
74    for i=1:rb
75      D(:,i) = median (abs(repmat(b(i,:),ra,1) - a),2);
76    end
77    % D = median((abs (repmat (permute(a,[1 3 2]), [1 rb 1]) - ...
78    %                  repmat (permute(b,[3 1 2]), [ra 1 1]))),3);
79  case 1,
80    for i=1:rb
81      D(:,i) = min (abs(repmat(b(i,:),ra,1) - a),[],2);
82    end
83%    D = min((abs (repmat (permute(a,[1 3 2]), [1 rb 1]) - ...
84%                  repmat (permute(b,[3 1 2]), [ra 1 1]))),[],3);
85  case ra,
86    for i=1:rb
87      D(:,i) = max (abs(repmat(b(i,:),ra,1) - a),[],2);
88    end
89%    D = max((abs (repmat (permute(a,[1 3 2]), [1 rb 1]) - ...
90%                  repmat (permute(b,[3 1 2]), [ra 1 1]))),[],3);
91  otherwise
92    for i=1:rb
93      aa = sort (abs(repmat(b(i,:),ra,1) - a),2);
94      D(:,i) = aa(:,k);
95    end
96%    aa = sort (abs (repmat (permute(a,[1 3 2]), [1 rb 1]) - ...
97%               repmat (permute(b,[3 1 2]), [ra 1 1])), 3);
98%    D  = aa(:,:,k);
99end
100
101% Check numerical inaccuracy
102D (find (D < eps)) = 0;   % Make sure that distances are nonnegative
103if bisa,
104  D = 0.5*(D+D');         % Make sure that distances are symmetric for D(A,A)
105end
106
107% Set object labels and feature labels
108if xor(isda, isdb),
109  prwarning(1,'One matrix is a dataset and the other not. ')
110end
111if isda,
112  if isdb,
113    D = setdata(A,D,getlab(B));
114  else
115    D = setdata(A,D);
116  end
117  D.name = 'Distance matrix';
118  if ~isempty(A.name)
119    D.name = [D.name ' for ' A.name];
120  end
121end
122return
123
124
125
126
127function k = whichk(kk,ca)
128if isstr(kk),
129  switch lower(kk),
130    case 'min'
131      k = 1;
132    case 'max'
133      k = ca;
134    case 'median',
135      k = 0;
136    otherwise
137      error ('Wrong parameter k.');
138  end
139elseif max(size(kk)) == 1,
140  k = kk;
141else
142  error ('Wrong parameter k.');
143end
144
145if k < 0 | k > ca,
146  error ('The parameter k, if an integer, must be positive and not larger then the number of features.');
147end
Note: See TracBrowser for help on using the repository browser.