How do I compare two sets of numerical strings character by character on Matlab ?
3 views (last 30 days)
Show older comments
I want to compare two sets of numerical data character by character e.g.
1011101 and 1011000
so that matlab will show that the 5 and 7th characters are different (0000101). I want to then count the number of differences (2).
If the terminology I use is wrong, please ignore that and focus on the example I have given to communicate what I want to do.
4 Comments
José-Luis
on 8 May 2014
To import excel data to Matlab:
doc xlsread
A somewhat faster option:
Accepted Answer
Star Strider
on 8 May 2014
Using MATLAB binary operations (considering the inputs — a and b here — are numbers:
a = 1011101;
b = 1011000;
A = num2str(a,'%d')
B = num2str(b,'%d')
ai = int16(bin2dec(A))
bi = int16(bin2dec(B))
C = dec2bin(bitxor(ai,bi))
HamDist = length(findstr(C,'1')) % Hamming Distance
produces:
HamDist =
2
What you want is known as the Hamming Distance between two binary numbers.
0 Comments
More Answers (1)
Jos (10584)
on 8 May 2014
a = '1011101'
b = '1011000'
q = a~=b % a logical array, true for locations where a and b differ
n = nnz(q) % count the number of non zeros (true)
7 Comments
Sagar Damle
on 10 May 2014
Edited: Sagar Damle
on 10 May 2014
Hannah,though you have accidentally clicked on Star's answer,now you can vote for Jos by clicking on '0 votes' written below Noddy's picture!This will add two points to Jos's reputation!
Jos (10584)
on 12 May 2014
Edited: Jos (10584)
on 12 May 2014
Thanks Sagar, especially for recognising Noddy! I do not care so much about reputation anymore, but I like feedback.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!