Azzera filtri
Azzera filtri

calculating hamming distance and total hamming distance

9 visualizzazioni (ultimi 30 giorni)
emad
emad il 15 Ott 2014
Risposto: Sufiyan il 8 Apr 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.

Risposte (1)

Sufiyan
Sufiyan il 8 Apr 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