calculating hamming distance and total hamming distance

25 visualizaciones (últimos 30 días)
emad
emad el 15 de Oct. de 2014
Respondida: Sufiyan el 8 de Abr. de 2023
hi How can I calculate hamming distance and total hamming distance for binary matrices with different rows, as a= [0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0; 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0; 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1; 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1];
Thank you in advance.

Respuestas (1)

Sufiyan
Sufiyan el 8 de Abr. de 2023
Hi,
You can refer to the code below to calculate hamming distance and total hamming distance for binary matrices.
% input matrix a
a = [0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0;
0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0;
0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1;
0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1];
d = pdist(a, 'hamming');
% Display the Hamming distances between rows as a square matrix
n = size(a, 1);
D = squareform(d);
D(logical(eye(n))) = NaN; % Set diagonal to NaN
disp('Hamming distances:')
Hamming distances:
disp(D)
NaN 0.4848 0.5152 0.4545 0.4848 NaN 0.5758 0.5152 0.5152 0.5758 NaN 0.3636 0.4545 0.5152 0.3636 NaN
% Calculate the total Hamming distance as the sum of all pairwise distances
total_d = sum(d);
disp(['Total Hamming distance: ' num2str(total_d)])
Total Hamming distance: 2.9091
you can refer to the pdist to get more information on pdist function.

Community Treasure Hunt

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

Start Hunting!

Translated by