フィルターのクリア

calculating hamming distance and total hamming distance

9 ビュー (過去 30 日間)
emad
emad 2014 年 10 月 15 日
回答済み: Sufiyan 2023 年 4 月 8 日
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.

回答 (1 件)

Sufiyan
Sufiyan 2023 年 4 月 8 日
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.

カテゴリ

Help Center および File ExchangeHamming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by