Thread Subject: Explicit Loops

Subject: Explicit Loops

From: Justin

Date: 8 Nov, 2009 23:22:02

Message: 1 of 4

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)
c=double(motif);
d=double(protein);
[rows cols]=size(d);
if rows~=1
    disp('motif_match only accepts vectors') %Error checking; proteins can not have multiple rows.
loc=[];
else
    a=1:length(c);
    while d(a)~=c
        loc=a+1;
        if d(loc)==c
            disp(loc(1))
            break
        end
    end
end

Subject: Explicit Loops

From: dpb

Date: 8 Nov, 2009 23:38:46

Message: 2 of 4

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
 >>

--

Subject: Explicit Loops

From: Justin

Date: 8 Nov, 2009 23:54:01

Message: 3 of 4

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

Subject: Explicit Loops

From: dpb

Date: 9 Nov, 2009 01:28:51

Message: 4 of 4

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.

--

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
function Justin 8 Nov, 2009 18:24:08
while Justin 8 Nov, 2009 18:24:08
for Justin 8 Nov, 2009 18:24:08
explicit loops Justin 8 Nov, 2009 18:24:07
rssFeed for this Thread

Contact us at files@mathworks.com