naive_string_matching.m

来自「Duda的《模式分类》第二版的配套的Matlab源代码」· M 代码 · 共 24 行

M
24
字号
function sh = Naive_String_Matching(text, x)

% Naive string matching: Find the location of the string x in text 'text'
%
% Inputs:
%	text				- Text vector
%	x					- Search string
%
% Output:
%	sh					- Locations of the string

n	= length(text);
m	= length(x);
s	= 0;
sh	= [];

while (s <= n - m),
   if strcmp(x, text(s+1:s+m)),
      disp(['The pattern occurs at shift ' num2str(s+1)])
		sh(end+1) = s+1;
   end
   s = s + 1;
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?