i have two binary array 10011 and 01010, i want compare each binary and find the number of different and similar bits?

 Accepted Answer

binary1 = 1st binary char vector
binary2 = 2nd binary char vector
number_of_similar_bits = sum(binary1==binary2);

7 Comments

thank you for answer James Tursa, but unfortunately didn't work with me
@Mahdi: Here are two examples with the binary values being character strings and double vectors,
>> binary1 = '10011'; % As character strings
>> binary2 = '01010';
>> number_of_similar_bits = sum(binary1==binary2)
number_of_similar_bits =
2
>> binary1 = [1 0 0 1 1]; % As numeric vectors
>> binary2 = [0 1 0 1 0];
>> number_of_similar_bits = sum(binary1==binary2)
number_of_similar_bits =
2
So, apparently you have something different from what I assumed. You need to tell us what this format is before we can help you.
My guess is on decimal numbers having been used, like ten thousand and eleven 10011
Then how did he get 01010 if he was using numbers instead of strings?
Thank you James Tursa this work with me
>> binary1 = [1 0 0 1 1]; % As numeric vectors
>> binary2 = [0 1 0 1 0];
>> number_of_similar_bits = sum(binary1==binary2)
It is common for people to enter decimal values such as 01010 intending them to indicate binary, and then to get frustrated and confused when MATLAB removes the leading zeros.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!