2

I have two equal length strings containing 1's and 0's. Each string is 128-bits long, and I want to calculate the Hamming distance between them. What's the best way I can go about doing this?

e.g. a='1000001' and b='1110001' --> dist=Hamming(a,b);

2 Answers 2

6

Use pdist with the hamming parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

Only thing to note is that this gives a fractional result, which is nonstandard. You'll need to multiply the result by length(a).
You also need to turn 1000001 into [1 0 0 0 0 0 0 1] in order to let pdist treat each digit separately
5
dist = sum(a ~= b);

2 Comments

Your solution is incorrect. Take dist = sum(4 ~= 8) produces 1, where in fact 0100 and 1000 have hamming distance 2.
@GustavoLitovsky: You missed that the OP has strings of bits, not integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.