How to find number of bit change between binary numbers?

29 vues (au cours des 30 derniers jours)
ASHA PON
ASHA PON le 29 Jan 2023
Commenté : ASHA PON le 29 Jan 2023
I am having 7 binary numbers. Now, i need to calculate the number of bit changes between adjacent binary numbers. Number of bit change between 110 and 101 is 2. Next 101 and 011 is 2, 011 and 111 is 1, 111 and 100 is 2 and so on. Thank you in advance.
For example:
A=[110, 101, 011, 111, 100, 001, 010]
Expected output:
B= 2, 2, 1, 2, 2, 2
  6 commentaires
Stephen23
Stephen23 le 29 Jan 2023
"This is the binary format of decimal number."
No, what you show in your question are some decimal number which just happen to use digits 1 and 0. MATLAB does not have a "binary format of decimal number". Binary bits can be stored as text, or of course as the binary data in a typically integer type) numeric.
ASHA PON
ASHA PON le 29 Jan 2023
@Star Strider yes I need to calculate hamming distance between these elements. Can you please suggest me some methods. I used D = pdist(A,'minkowski',1), but I didn't get correct output.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 29 Jan 2023
A = ['110'; '101'; '011'; '111'; '100'; '001'; '010']
A = 7×3 char array
'110' '101' '011' '111' '100' '001' '010'
B = sum(diff(A,1,1)~=0,2)
B = 6×1
2 2 1 2 2 2

Plus de réponses (2)

Jan
Jan le 29 Jan 2023
Modifié(e) : Jan le 29 Jan 2023
Step 1: Convert your input to a typical binary representation as a numerical vector [1,1,0] or char vector '110'.
Step 2: XOR the values.
Step 3: Count the bits.
A = {'110', '101', '011', '111', '100', '001', '010'};
XA = myXOR(A(1:end-1), A(2:end));
N = myBitCount(XA);
function X = myXOR(A, B)
% A and B must be cell strings, which elements must have the same number of
% characters. Otherwise the function stops with an error or replies a wrong
% result!
X = cell(size(A));
for k = 1:numel(A)
X{k} = (A{k} == B{k}); % Elementwise comparison
end
end
function N = myBitCount(A)
N = zeros(size(A));
for k = 1:numel(A)
N(k) = sum(A{k} == '1');
end
end
But if your input format differs from a cell string of binary char vectors, typing this was a waste of time.
  2 commentaires
ASHA PON
ASHA PON le 29 Jan 2023
@Jan Actually I am having only one matrix, but while using 'xor' command you suggested A and B. Can you please explain me how you have assumed B.
Jan
Jan le 29 Jan 2023
@ASHA PON: You still do not want to answer my question for clarification, what your input is. How surprising. This is your question and you should be interested in finding a working solution. With letting the readers guess, what your inputs are, you make it harder to help you.
My code compares A(1:end-1) with A(2:end). Inside myXOR the first part is called "A" and the second part "B".

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 29 Jan 2023
Modifié(e) : John D'Errico le 29 Jan 2023
Read what @Jan said. xor followed by sum is the simple way to go, of course. And we really have not been given enough information to know how to best solve your problem, because we don't know the actual problem you have. That is, is your problem space always composed of a sequence of 7 binary numbers, ALWAYS composed of 3 binary bits? If so, then the answer is trivial. Just use a lookup table.
Store in an 8x8 array, the number of bit differences between each pair of possible binary numbers. (There are only 8 possible 3 bit integers.) Or, if you prefer, use a hash table to do the work for you and the lookups. Computee all possible differences in advance. And there are only 8*8=64 possible pairs to worry about, so the table is trivial to build. Then the lookup is now trivial, however you now choose to do it.
But if your real problem actually is composed of bit strings that may have arbitrarily more than 3 bits, then you do need to count the number of bit changes, using xor and then sum as Jan suggests.
Edit: It seems you are still confused. So here is exactly what I would do.
[N,M] = ndgrid(0:7);
Nbin = dec2bin(N) - '0';
Mbin = dec2bin(M) - '0';
bitDiffLUT = reshape(sum(xor(Nbin,Mbin),2),8,8)
bitDiffLUT = 8×8
0 1 1 2 1 2 2 3 1 0 2 1 2 1 3 2 1 2 0 1 2 3 1 2 2 1 1 0 3 2 2 1 1 2 2 3 0 1 1 2 2 1 3 2 1 0 2 1 2 3 1 2 1 2 0 1 3 2 2 1 2 1 1 0
What I have created here is a simple lookup table, that allows us to compute the number of bits that differ between ANY pair of binary 3 bit numbers.
For example, how many bits differ between the binary numbers 101 and 011? Simple. Convert them to decimal. Add 1, then lookup the number of bits that differ.
101 = 5 as a decimal number. 011 is 3 as a decimal number. Since MATLAB is a origin 1 language, we add 1 to each result. Then index into the table.
A = '101';B = '011';
bitDiffLUT(bin2dec(A)+1,bin2dec(B)+1)
ans = 2
Indeed, there are exactly 2 bits that differ between the pair of binary numbers.
Could I have done it in another way? Of course. I could have used a dictionary. There are many ways to achieve this.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by