user6594048
user6594048

Reputation:

How to calculate Hamming distance in matlab for two vectors?

I need to calculate the hamming distance between two vectors in octave. Searching in the internet, I found that Hamming distance by just using this function: pdist (), But it doesn't give me any result, because pdist() is a missing function in octave.

w= pdist([208    15   217   252   128    35    50   252   209   120    97   140   235   220    32   251],
       [231   174   143    43   125    66    49   143    48   139    81   103   154   229    93   229],1)

I would be very grateful if you could help me please.

Upvotes: 0

Views: 3884

Answers (2)

Keshav Naram
Keshav Naram

Reputation: 1

In matlab we can do it like this:

function dist = ham_dist(a,b,min_length)
%hamming distance of a, b. a and b are strings of decimal numbers respectively.
a = a*1-48;
b = b*1-48;
dist = sum(bitxor(a,b),2);
end

basically it is used a*1-48 is converting a binary string to row vector so that we can use bitxor. bitxor gives 1 at the positions they (a,b) differ, and 0 elsewhere. taking sum of those gives hamming distance which is the count of no. of places where two binary numbers differ.

Upvotes: 0

awpathum
awpathum

Reputation: 239

  • Hamming distance is a metric for comparing two binary data strings.
  • While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different.

Hamming distance with xor and sum

a = [1, 1, 0, 0];

b = [1, 0, 1, 0];

diff = xor(a,b)

d = sum(diff)

answer will be :

d = 2

Upvotes: 1

Related Questions