Comparing two different strings
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Stephen23
2018-1-16
>> 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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!