Path: news.mathworks.com!newsfeed-00.mathworks.com!oleane.net!oleane!news.ecp.fr!feeder.eternal-september.org!eternal-september.org!not-for-mail
From: dpb <none@non.net>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Explicit Loops
Date: Sun, 08 Nov 2009 19:28:51 -0600
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <hd7r8q$57a$1@news.eternal-september.org>
References: <hd7jqq$1h4$1@fred.mathworks.com> <hd7kqd$g98$1@news.eternal-september.org> <hd7lmp$p6d$1@fred.mathworks.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.eternal-september.org U2FsdGVkX1+09fJLBXYUE8kXNxJOlY/DGPmjI3N9ENDg+Y2qT0fpyHHIigItD5q5iOQfv0t0RBOg0UIpQJ3IfQCmmaDbpcLGupIjjN8fk4vkmZGu+3kXDi7Bw4NpaRs5jZQP9PUvGqY=
X-Complaints-To: abuse@eternal-september.org
NNTP-Posting-Date: Mon, 9 Nov 2009 01:28:59 +0000 (UTC)
In-Reply-To: <hd7lmp$p6d$1@fred.mathworks.com>
X-Auth-Sender: U2FsdGVkX1/p8bJ7doSqdFyrveQ2efUmqNVwDhnBOzA=
Cancel-Lock: sha1:2ZlBJb4hkneWKF9qvv8pAy+LexU=
User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
Xref: news.mathworks.com comp.soft-sys.matlab:583446


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.

--