source: distools/hamdistm.m @ 63

Last change on this file since 63 was 10, checked in by bduin, 14 years ago
File size: 1.4 KB
Line 
1%HAMDISTM Hamming Distance Matrix between Binary Vectors
2%
3%   D = HAMDISTM(A,B)
4%       OR
5%   D = HAMDISTM(A)
6%
7% INPUT
8%   A   NxK Binary matrix or dataset
9%   B   MxK Binary matrix or dataset
10%
11% OUTPUT
12%   D   NxM Dissimilarity matrix or dataset
13%
14% DESCRIPTION
15% Hamming distance between sets of binary vectors.
16% If A and B are datasets, then D is a dataset as well with the labels defined
17% by the labels of A and the feature labels defined by the labels of B. If A is
18% not a dataset, but a matrix of doubles, then D is also a matrix of doubles.
19%
20
21% Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com
22% Faculty EWI, Delft University of Technology and
23% School of Computer Science, University of Manchester
24
25
26function d = hamdistm(A,B)
27bisa = nargin < 2;
28if bisa,
29  B = A;
30end
31
32isda = isdataset(A);
33isdb = isdataset(B);
34a = +A;
35b = +B;
36
37[ra,ca] = size(a);
38[rb,cb] = size(b);
39if ca ~= cb,
40  error ('Matrices should have equal numbers of columns');
41end
42
43if any(a~=0) | any(a~=1) | any(b~=0) | any(b~=1),
44  error('Data should be binary.');
45end
46
47D = zeros(ra,rb);
48for i=1:rb
49  D(:,i) = sum((repmat(b(i,:),ra,1) ~= a),2);
50end
51
52% Set object labels and feature labels
53if xor(isda, isdb),
54  prwarning(1,'One matrix is a dataset and the other not. ')
55end
56if isda
57  if isdb,
58    D = setdata(A,D,getlab(B));
59  else
60    D = setdata(A,D);
61  end
62  D.name = 'Distance matrix';
63  if ~isempty(A.name)
64    D.name = [D.name ' for ' A.name];
65  end
66end
67return
Note: See TracBrowser for help on using the repository browser.