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 | |
---|
26 | function d = hamdistm(A,B) |
---|
27 | bisa = nargin < 2; |
---|
28 | if bisa, |
---|
29 | B = A; |
---|
30 | end |
---|
31 | |
---|
32 | isda = isdataset(A); |
---|
33 | isdb = isdataset(B); |
---|
34 | a = +A; |
---|
35 | b = +B; |
---|
36 | |
---|
37 | [ra,ca] = size(a); |
---|
38 | [rb,cb] = size(b); |
---|
39 | if ca ~= cb, |
---|
40 | error ('Matrices should have equal numbers of columns'); |
---|
41 | end |
---|
42 | |
---|
43 | if any(a~=0) | any(a~=1) | any(b~=0) | any(b~=1), |
---|
44 | error('Data should be binary.'); |
---|
45 | end |
---|
46 | |
---|
47 | D = zeros(ra,rb); |
---|
48 | for i=1:rb |
---|
49 | D(:,i) = sum((repmat(b(i,:),ra,1) ~= a),2); |
---|
50 | end |
---|
51 | |
---|
52 | % Set object labels and feature labels |
---|
53 | if xor(isda, isdb), |
---|
54 | prwarning(1,'One matrix is a dataset and the other not. ') |
---|
55 | end |
---|
56 | if 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 |
---|
66 | end |
---|
67 | return |
---|