calculating hamming distance and total hamming distance
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (1)
Sufiyan
le 8 Avr 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:')
disp(D)
% Calculate the total Hamming distance as the sum of all pairwise distances
total_d = sum(d);
disp(['Total Hamming distance: ' num2str(total_d)])
0 commentaires
Voir également
Catégories
En savoir plus sur Hamming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!