How do I compare two sets of numerical strings character by character on Matlab ?

조회 수: 4 (최근 30일)
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
Hannah
Hannah 2014년 5월 10일
편집: Hannah 2014년 5월 10일
Thanks,
I'm having some trouble though because the numbers are too long so if I use xlsread, the number is coming up as NaN. Any ideas on how to solve this?
p.s. I managed to upload the file as a cvs manually, but then Matlab is having trouble processing the logical array ~=

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2014년 5월 8일
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.

추가 답변 (1개)

Jos (10584)
Jos (10584) 2014년 5월 8일
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
Sagar Damle
Sagar Damle 2014년 5월 10일
편집: Sagar Damle 2014년 5월 10일
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)
Jos (10584) 2014년 5월 12일
편집: Jos (10584) 2014년 5월 12일
Thanks Sagar, especially for recognising Noddy! I do not care so much about reputation anymore, but I like feedback.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by