Skip to content

Instantly share code, notes, and snippets.

@obstschale
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obstschale/9191577 to your computer and use it in GitHub Desktop.
Save obstschale/9191577 to your computer and use it in GitHub Desktop.
Calculate Levenstein distance using Octave function
%% 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