Hello,
I'm writing a function which returns the sequences of n requested letters in a string. For example, :
string = 'abcad' , n=3
ans =
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:
ngramsFreq('abcad',3)
ans =
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].

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 16 Avr 2013
Modifié(e) : Andrei Bobrov le 16 Avr 2013

0 votes

str = 'abcad' ;
n=3;
out = str( hankel(1:n,n:numel(str)) );
without hankel
out = str( bsxfun(@plus,1:n,(0:numel(str) - n)') )
with while-loop
g = 1:n;
out = [];
while g(end) <= numel(str)
out = [out;str(g)];
g = g + 1;
end

3 commentaires

googo
googo le 16 Avr 2013
Hey, I can't use this (not allowed). You can help me fix the code that I wrote?
Thank's.
Andrei Bobrov
Andrei Bobrov le 16 Avr 2013
added variant with while-loop
googo
googo le 16 Avr 2013
Thank's, I'll try this too.

Connectez-vous pour commenter.

Plus de réponses (2)

Yao Li
Yao Li le 16 Avr 2013

0 votes

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;
t(i,1:n)=string(k:1:(k+n)-1);
end
s=t;
end

7 commentaires

googo
googo le 16 Avr 2013
Thank's!
Could you explain me a little bit about the meaning of string(m:n:t) for what m,n,t sign for? (very new in programming, excuse me :))
Yao Li
Yao Li le 16 Avr 2013
string(m:n:t) means from the mth letter to the tth letter with a increasement (or decreasement) of n
googo
googo le 16 Avr 2013
Modifié(e) : googo le 16 Avr 2013
OK, thank's! I'm trying to erase the doubles using unique function and keeping the same order. For example : abc abd and n=2
ab
bc
c
a
bd
ab is showen only once.
Any way to do this? I'm using 2011 version. Thank's!
Yao Li
Yao Li le 16 Avr 2013
Why there are c and a since n=2? Is the string='abcabd' or there are two strings, 1st='abc' and 2nd='abd'?
googo
googo le 16 Avr 2013
I consider space as a tab also.
Yao Li
Yao Li le 16 Avr 2013
You can divide your algorithm to 2 steps: 1st find the specific group of letters. 2nd remove the repeated groups
googo
googo le 16 Avr 2013
Modifié(e) : googo le 16 Avr 2013
Thank's. I did like this:
[~,index] = unique(t,'first','rows');
s = t(sort(index),1:n);

Connectez-vous pour commenter.

0 votes

Hi
I am writing a code for calculating a parameter with if clauses. How does the code can return when it does not satisfy my if calculation?
thank you

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by