MATLAB calculates the Hamming distance between two sets

Source: Internet
Author: User

I. Description of the problem

B1[1 2 3

4 5 6

7 8 9]

B2[12 13 14

21 31 41

51 1 1

81 1 1]

Two decimal matrices, not the same number of rows, respectively, is N1 and N2, the number of columns must be consistent, for nwords, the output matrix DH is [N1,N2], which is the same as seeking two true Euclidean distance.

Outputs [1 1] = 1 and 12 Hamming +2 and 13 Hamming + 3 and 14 Hamming, [1 2] = 1 and 21 + 2 and 31 + 3 and 41, that is, [I j] is the Hamming distance of B1 line I and B2 J.

Two. Problem analysis

1 and 12 21 51 81 for the Hamming distance, the defense of a (a course), 2 and 13 31 1 1 for the Hamming distance, placed in B;3 and 14 41 1 1 defense c; then a+b+c, the Hamming distance between the first row in the B1 and the rows in the B2 are obtained.

In order to speed up the operation, the table can be played, the number of two nwords=8 or 0 to 255 (Bitxor XOR or 10 forbidden number), so 0 to 255 a total of 256 number each number contains a few 1 (that is, Hamming distance) exists in a matrix Bit_in_char.

Three. Implement

function Dh=hammingdist (B1, B2) percent Compute Hamming distance between, sets of samples (B1, B2)% dh=hammingdist (B1, B2);% % input% B1, b2:compact bit vectors.  Each datapoint was one row.% size (B1) = [Ndatapoints1, nwords]% size (B2) = [Ndatapoints2, nwords]% It is faster if Ndatapoints1 < ndatapoints2%% output% Dh = Hamming distance. % size (Dh) = [Ndatapoints1, ndatapoints2]% example query% Dhamm = Hammingdist (B2, B1);% this would give the same result than:% Dhamm = Distmat (u2>0, u1>0). ^2;% the size of the distance matrix is:% size (Dhamm) = [Ntest x ntraining]    % loop-up Table:bit_in_char = UInt16 ([...    0112122312232334122323...    3423343445122323342334...    3445233434453445455612...    2323342334344523343445...    3445455623343445344545...    5634454556455656671223...    2334233434452334344534...4545562334344534454556...    3445455645565667233434...    4534454556344545564556...    5667344545564556566745... 5 6 5 6 6 7 5 6 6 7 6 7 7 8]); n1 = size (b1,1);        [N2, Nwords] = size (B2);D h = zeros ([n1 n2], ' uint16 '); for j = 1:n1 for n=1:nwords y = Bitxor (B1 (j,n), B2 (:, n));    DH (j,:) = DH (j,:) + Bit_in_char (y+1); EndEnd

MATLAB calculates the Hamming distance between two sets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.