i have two binary array 10011 and 01010, i want compare each binary and find the number of different and similar bits?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
Accepted Answer
James Tursa
on 21 Sep 2018
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
How do you initialize your binary array storing 10011
@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.
Walter Roberson
on 24 Sep 2018
Edited: Walter Roberson
on 24 Sep 2018
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?
mahdi alhasani
on 25 Sep 2018
Edited: Walter Roberson
on 25 Sep 2018
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.
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Tags
See Also
on 21 Sep 2018
on 25 Sep 2018
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)