source: distools/simdistm.m @ 69

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