source: distools/samdistm.m @ 10

Last change on this file since 10 was 10, checked in by bduin, 14 years ago
File size: 2.4 KB
Line 
1%SAMDISTM  Distance matrix based on Spectral Angular Mapper (SAM)
2%          distance, which is also the spherical geodesic distance
3%
4%   D = SAMDISTM (A,B,R)
5%     OR
6%   D = SAMDISTM (A,B)
7%     OR
8%   D = SAMDISTM (A,R)
9%     OR
10%   D = SAMDISTM (A)
11%
12% INPUT
13%   A   NxK matrix (dataset)
14%   B   MxK matrix (dataset)
15%   R   Radius (optional, default: 1)
16%
17% OUTPUT
18%   D   NxM dissimilarity matrix (dataset)
19%
20% DESCRIPTION
21% Computes the distance matrix D between two sets of vectors, A and B.
22% Distances between vectors X and Y are computed based on the spherical
23% geodesic formula:
24%     D(X,Y) = R arcos (X'Y/R^2)
25% X and Y are normalized to a unit length.
26%
27% If A and B are datasets, then D is a dataset as well with the labels defined
28% by the labels of A and the feature labels defined by the labels of B. If A is
29% not a dataset, but a matrix of doubles, then D is also a matrix of doubles.
30%
31% DEFAULT
32%   R = 1
33%
34% REMARKS
35% A square SAM-distance D(A,A) for a finite set A can be proved to be
36% the l_1-distance (LPDISTM). D(A,A).^{1/2} has a Euclidean behavior, so
37% it can be embedded by PSEM in a Euclidean space.
38%
39
40% SEE ALSO
41% JACSIMDISTM, CORRDISTM, LPDISTM, DISTM
42
43% Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com
44% Faculty EWI, Delft University of Technology and
45% School of Computer Science, University of Manchester
46
47
48function D = samdistm (A,B,r)
49
50bisa = 0;
51if nargin < 2,
52  r = 1;
53  B = A;
54  bisa = 1;
55else
56  if nargin < 3,
57    if max (size(B)) == 1,
58      r = B;
59      B = A;
60      bisa = 1;
61    else
62      r = 1;
63    end
64  end
65end
66
67if r <= 0,
68  error ('The parameter R must be positive.');
69end
70
71isda = isdataset(A);
72isdb = isdataset(B);
73a    = +A;
74b    = +B;
75[ra,ca] = size(a);
76[rb,cb] = size(b);
77
78if ca ~= cb,
79  error ('The matrices should have the same number of columns.');
80end
81
82aa = sum(a.*a,2);
83bb = sum(b.*b,2)';
84D  = (a*b') ./sqrt(aa(:,ones(rb,1)) .* bb(ones(ra,1),:));
85D  = r * acos(D/r^2);
86
87% Check numerical inaccuracy
88D (find (D < eps)) = 0;   % Make sure that distances are nonnegative
89if bisa,
90  D = 0.5*(D+D');         % Make sure that distances are symmetric for D(A,A)
91end
92
93
94% Set object labels and feature labels
95if xor(isda, isdb),
96  prwarning(1,'One matrix is a dataset and the other not. ')
97end
98if isda,
99  if isdb,
100    D = setdata(A,D,getlab(B));
101  else
102    D = setdata(A,D);
103  end
104  D.name = 'Distance matrix';
105  if ~isempty(A.name)
106    D.name = [D.name ' for ' A.name];
107  end
108end
109return
Note: See TracBrowser for help on using the repository browser.