|
Justin wrote:
> dpb <none@non.net> wrote in message <hd7kqd$g98$1@news.eternal-september.org>...
>> Justin wrote:
>>> What I want to do is write a function that will return all
>>> locations at which a random chain of letters appears in a longer chain
>>> of letters.
>>> So if the smaller chain of random letter, "motif" is aar and the
>>> longer chain, "protein" is abbdaardkadaar, the function will return the
>>> location of aar at 5 and 12. Here's what I have so far, but something
>>> is off. I would really appreciate some assistance. Thank so much!
>>> function loc=Motif_Find(motif,protein)
>> loc = findstr(protein, motif);
>>> end
>> Example from command window...
>>
>> >> p = 'abbdaardkadaar';
>> >> m = 'aar';
>> >> findstr(p,m)
>> ans =
>> 5 12
>> >>
>>
>> --
>
> Oh right. Thanks. But is there a way to do this just using explicit
> looping? I'm trying to practice my looping and I want to be able to
> understand the mechanics behind findstr
Well, of course, just work your way through the target string w/ the
string to match on character at a time.
jdx = 0;
lp = length(p); lm = length(m);
for idx=1:lp-lm
if strcmp(p(idx:idx+lm-1), m)
jdx = jdx+1;
loc(jdx) = idx;
end
end
The above could be streamlined by making the step skip over the length
of the searched for string if found rather than looking at every
possible location and similar efficiencies but gives the idea.
--
|