I'm writing a function which returns the sequences of n requested letters in a string. For example, :
string = 'abcad' , n=3
abc bca cad
Well, my code is :
function s=ngramsFreq(string,n) k=0; t = repmat(char(0),length(string)-n+1,n); for i=1:(length(string)-n)+1 k=k+1; for j=1:n for m=k:(k+n)-1 t(i,j)=string(m); break end end end s=t; end
When I'm runing the program for 'abcad', n=3 the function returns:
aaa bbb ccc
I think the problem is in the inside loop:
for m=k:(k+n)-1 t(i,j)=string(m); break end
I want it to end after one step and not keep looping. For example After t(1,1)=string(1) go to j=2 and not return to m=1.
Any help with this? thank you very much!
Note: For the meantime, if a sequence is showing twice or more the function will return both sequences or how many there is (for example: 'aaa' will return [aa;aa].
No products are associated with this question.
str = 'abcad' ; n=3; out = str( hankel(1:n,n:numel(str)) );
out = str( bsxfun(@plus,1:n,(0:numel(str) - n)') )
g = 1:n; out = ; while g(end) <= numel(str) out = [out;str(g)]; g = g + 1; end
Play games and win prizes!Learn more