[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 | |
---|
| 44 | function D = rankdistm (A,B,kk) |
---|
| 45 | bisa = 0; |
---|
| 46 | if nargin == 3, |
---|
| 47 | k = whichk(kk,ca); |
---|
| 48 | elseif nargin < 2, |
---|
| 49 | k = 0; % median |
---|
| 50 | B = A; |
---|
| 51 | bisa = 1; |
---|
| 52 | else |
---|
| 53 | k = whichk(B,ca); |
---|
| 54 | B = A; |
---|
| 55 | bisa = 1; |
---|
| 56 | end |
---|
| 57 | |
---|
| 58 | isda = isdataset(A); |
---|
| 59 | isdb = isdataset(B); |
---|
| 60 | a = +A; |
---|
| 61 | b = +B; |
---|
| 62 | |
---|
| 63 | [ra,ca] = size(a); |
---|
| 64 | [rb,cb] = size(b); |
---|
| 65 | |
---|
| 66 | if ca ~= cb, |
---|
| 67 | error ('The matrices should have the same number of columns.'); |
---|
| 68 | end |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | D = zeros(ra,rb); |
---|
| 72 | switch 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); |
---|
| 99 | end |
---|
| 100 | |
---|
| 101 | % Check numerical inaccuracy |
---|
| 102 | D (find (D < eps)) = 0; % Make sure that distances are nonnegative |
---|
| 103 | if bisa, |
---|
| 104 | D = 0.5*(D+D'); % Make sure that distances are symmetric for D(A,A) |
---|
| 105 | end |
---|
| 106 | |
---|
| 107 | % Set object labels and feature labels |
---|
| 108 | if xor(isda, isdb), |
---|
| 109 | prwarning(1,'One matrix is a dataset and the other not. ') |
---|
| 110 | end |
---|
| 111 | if 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 |
---|
| 121 | end |
---|
| 122 | return |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | function k = whichk(kk,ca) |
---|
| 128 | if 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 |
---|
| 139 | elseif max(size(kk)) == 1, |
---|
| 140 | k = kk; |
---|
| 141 | else |
---|
| 142 | error ('Wrong parameter k.'); |
---|
| 143 | end |
---|
| 144 | |
---|
| 145 | if k < 0 | k > ca, |
---|
| 146 | error ('The parameter k, if an integer, must be positive and not larger then the number of features.'); |
---|
| 147 | end |
---|