How to measure the hamming distnace between the rows of a matrix?

9 views (last 30 days)
hello,
how can I find the hamming distance among the rows of a matrix A without repeation. For example:
A=[ 0,1,1,1,0,1,0;
0,1,0,1,1,1,1;
1,1,0,1,0,0,0;
1,1,1,1,1,1,0;
1,0,0,0,1,1,0 ]
then, I want to get the hamming distance of rows (1,2), (1,3), (1,4), (1,5) , (2,3), (2,4), (2,5), (3,4), (3,5), and (4,5).
So, how we could do that?
Regards,

Accepted Answer

Star Strider
Star Strider on 22 Mar 2020
Use the pdist2 function, and specifically the Distance argument to specify 'hamming' as the metric.
Example —
A=[ 0,1,1,1,0,1,0;
0,1,0,1,1,1,1;
1,1,0,1,0,0,0;
1,1,1,1,1,1,0;
1,0,0,0,1,1,0 ];
for k1 = 1:size(A,1)-1
for k2 = k1+1:size(A,1)
D(k1,k2) = pdist2(A(k1,:),A(k2,:), 'hamming');
end
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!