📄 p_spectrum_bf.m
字号:
function [result] = p_spectrum_bf(s,t,p)%P_SPECTRUM_BF% -Finds the contiguous subsequence match count between strings s and t% by using a brute-force approach, where the length of the subsequence is p.% *(There is also a dynamic programming implementation of this algorithm.% Type help p_spectrum for info.)%% -The following algorithm is used:% K[p](s,t) = [Summation of i from 1 to |s|-p+1]% [Summation of j from 1 to |t|-p+1]% delta(s(i:i+p+1),t(j:j+p+1)),%% (where delta is the identity function, which returns% 1 if the arguments are equal, and 0 otherwise.)%% -Example: p_spectrum_bf('abccc','abc', 3) returns a value of 1.% (Note that p_spectrum_bf('abccc','abc',3)=p_spectrum_bf('abc','abccc',3) since K(s,t,p) = K(t,s,p) ).% -Example: p_spectrum_bf('a','a', 1) returns a value of 1.% -Example: p_spectrum_bf('a','b', 1) returns a value of 0.% -Example: p_spectrum_bf('ab','ab', 2) returns a value of 1.% %%%USAGE: scalar = p_spectrum_bf('string1','string2', p); (where p is the length of the substring)%%%%For more information, visit http://www.kernel-methods.net/%%Written and tested in Matlab 6.0, Release 12.%Copyright 2003, Manju M. Pai 4/2003%manju@kernel-methods.net%------------------------------------------------------------------------------------------%Obtain lengths of strings[num_rows_s, n] = size(s);[num_rows_t, m] = size(t);%Initialize result variableresult = 0;%Error checking statements: %Make sure input vectors are horizontal. if (num_rows_s ~= 1 | num_rows_t ~= 1) error('Error: s and t must be horizontal vectors.'); end; %If p is less than zero or not a number, program should quit due to faulty variable input. if p <= 0 | ischar(p) error('Error: p needs to be a number greater than 0.'); end;%End of error checking%Implement the algorithm with double 'for' loops.for i=1:(n-p+1) for j=1:(m-p+1) result = result + strcmpi( s(i:(i+p-1)), t(j:(j+p-1)) ); end;end;%End of algorithm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -