フィルターのクリア

How to compute number of bit change among consecutive binary numbers?

3 ビュー (過去 30 日間)
ASHA PON
ASHA PON 2022 年 12 月 14 日
コメント済み: Star Strider 2022 年 12 月 16 日
I am having 7 random binary numbers of 3 bit. Now, I need to compute how many number of bits are changed among consecutive numbers.
Example:
A= 110; 101; 011; 111; 100; 001; 010
Expected output:
C=
110 101 011 111 100 001 010
110 - 2 2 1 1 3 1
101 2 - 2 1 1 1 3
011 2 2 - 1 3 2 1
111 1 1 1 - 2 2 2
100 1 1 3 2 - 2 2
001 3 1 1 2 2 - 2
010 1 3 1 2 2 2 -

回答 (2 件)

Voss
Voss 2022 年 12 月 14 日
% A = 110; 101; 011; 111; 100; 001; 010
A = dec2bin([6; 5; 3; 7; 4; 1; 2],3)
A = 7×3 char array
'110' '101' '011' '111' '100' '001' '010'
N = size(A,1);
C = zeros(N);
for ii = 1:N
for jj = 1:N
C(ii,jj) = nnz(A(ii,:) ~= A(jj,:));
end
end
disp(C);
0 2 2 1 1 3 1 2 0 2 1 1 1 3 2 2 0 1 3 1 1 1 1 1 0 2 2 2 1 1 3 2 0 2 2 3 1 1 2 2 0 2 1 3 1 2 2 2 0
  10 件のコメント
ASHA PON
ASHA PON 2022 年 12 月 16 日
Actually while forming yy matrix, my size will be 6*7. But i need 7*7, so i included 0 diagonally to proceed with further calculation.
Voss
Voss 2022 年 12 月 16 日
yy is 7x7 before you remove the zeros, as shown above.

サインインしてコメントする。


Star Strider
Star Strider 2022 年 12 月 15 日
Use pdist with the 'hamming' distance metric then squareform to create the matrix —
b = {'110' '101' '011' '111' '100' '001' '010'};
d = pdist(b, 'hamming');
Warning: Converting cell data to double.
dmtx = squareform(d*3); % Differences Are Reported As Percentages, So Multiply By The Length Of The Strings To Get Integers
VNRN = cellfun(@(x)sprintf('%s',x),b, 'Unif',0);
Result = array2table(dmtx, 'VariableNames',VNRN, 'RowNames',VNRN) % Create A 'table' To Get The Desired Result
Result = 7×7 table
110 101 011 111 100 001 010 ___ ___ ___ ___ ___ ___ ___ 110 0 2 2 1 1 3 1 101 2 0 2 1 1 1 3 011 2 2 0 1 3 1 1 111 1 1 1 0 2 2 2 100 1 1 3 2 0 2 2 001 3 1 1 2 2 0 2 010 1 3 1 2 2 2 0
The variable names and row names do not have to be valid MATLAB variable names, however if you want to use them as such, change the ‘VNRN’ function to:
VNRN = cellfun(@(x)sprintf('%_s',x),b, 'Unif',0);
or something similar so that they all begin with a non-numeric character.
.
  2 件のコメント
ASHA PON
ASHA PON 2022 年 12 月 16 日
Thank you for the reply. Actually, I have solved the problem.
Star Strider
Star Strider 2022 年 12 月 16 日
My pleasure!

サインインしてコメントする。

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by