I would like to calculate the Hamming distance between two strings of variable length in Matlab. For fixed length strings the following syntax solves my problem:
str1 = 'abcde';
str2 = 'abedc';
sum(str1 ~= str2)
ans = 2
How can I do this efficiently for variable length strings?
Thank you!
EDIT: Because this is a legitimate question: For every character one string is longer then the other, the Hamming distance should be incremented. So for example for
str1 = 'abcdef';
str2 = 'abc';
The answer should be 3.
Here's a way to do it:
str1 = 'abcdef';
str2 = 'abc';
clear t
t(1,:) = str1+1; % +1 to make sure there are no zeros
t(2,1:numel(str2)) = str2+1; % if needed, this right-pads with zero or causes t to grow
result = sum(t(1,:)~=t(2,:));