Last active
August 29, 2015 13:56
-
-
Save obstschale/9191577 to your computer and use it in GitHub Desktop.
Calculate Levenstein distance using Octave function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Levenstein Distance: calculate levenstein distance with matrix | |
function [distance] = levenstein_distance(x, y, output = 0) | |
% initialization | |
ma = zeros( size(y,2)+1, size(x,2)+1 ); | |
ma(1,:) = [0:size(x,2)]; | |
ma(:,1) = [0:size(y,2)]; | |
for i=2:size(ma,1) | |
for j=2:size(ma,2) | |
ma(i,j) = min( [ ma(i,j-1)+1, ma(i-1,j)+1, ma(i-1,j-1)+1- (y(i-1)==x(j-1)) ] ); | |
end | |
end | |
% print matrix if wanted | |
if output ma end | |
distance = ma(end, end); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment