Comparing two different strings
8 views (last 30 days)
Show older comments
Hello, this is my first post so please go easy on me.
I want to compare two strings which I obtained from an OCR, but I need to do it letter by letter. The catch is that one string may have some missing letters or some unwanted characters compared to the other.
My first idea was to use strsplit to separate the two strings in words, but after that I've no clue what to do next.
Thanks a lot!
0 Comments
Accepted Answer
More Answers (1)
Stephen23
on 16 Jan 2018
>> C = {'live','eve','believe','belive'};
>> cellfun(@(c)wfEdits(c,'beleive'),C)
ans =
3 4 2 1
Thus showing that 'belive' is the closest to 'beleive'. The function is:
function d = wfEdits(S1,S2)
% Wagner–Fischer algorithm to calculate the edit distance / Levenshtein distance.
%
N1 = 1+numel(S1);
N2 = 1+numel(S2);
%
D = zeros(N1,N2);
D(:,1) = 0:N1-1;
D(1,:) = 0:N2-1;
%
for r = 2:N1
for c = 2:N2
D(r,c) = min([D(r-1,c)+1, D(r,c-1)+1, D(r-1,c-1)+~strcmpi(S1(r-1),S2(c-1))]);
end
end
d = D(end);
%
end
0 Comments
See Also
Categories
Find more on Characters and Strings 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!