Thread Subject: Function: location

Subject: Function: location

From: Justin

Date: 10 Nov, 2009 05:18:00

Message: 1 of 3

Objective: Trying to write a function that pinpoints the location at which a smaller chain of letters "ex. 'AB' " appears in a longer chain of letters "ex. 'BCDCABDEDAB' " and therefore, the function would return 5 and 10. Here's my coding for the function.
Any of matlab's built-in functions are not allowed. Only explicit looping can be used.
PART I
function a=Motif_Match(motif,protein,location)
[rows cols]=size(protein);
i=location:location+length(motif)-1; %cut the protein vector into the
%same length as the motif vector given the input location
if rows~=1
    disp('motif_match only accepts vectors') %Error checking; proteins can not have multiple rows.
elseif motif==protein(i) %protein of length of motif, starting at designated location, must be the
    %same as motif to be true.
            a=1;
else a=0; %if the aforementioned conditions do not apply, then the statement is false
end

Part II
function loc=Motif_Find(motif,protein)
[rows cols]=size(protein);
if rows~=1
    disp('motif_match only accepts vectors') %Error checking; proteins can not have multiple rows.
loc=[];
else
    loc=1;
    i=1;
    while i<=length(protein)-3;
        loc=Motif_Match(motif,protein,loc)*i;
        i=i+1;
    end
end

Subject: Function: location

From: Jos

Date: 10 Nov, 2009 07:33:03

Message: 2 of 3

"Justin " <riley127099@yahoo.com> wrote in message <hdat28$6qk$1@fred.mathworks.com>...
> Objective: Trying to write a function that pinpoints the location at which a smaller chain of letters "ex. 'AB' " appears in a longer chain of letters "ex. 'BCDCABDEDAB' " and therefore, the function would return 5 and 10. Here's my coding for the function.
> Any of matlab's built-in functions are not allowed. Only explicit looping can be used.
> PART I
> function a=Motif_Match(motif,protein,location)
> [rows cols]=size(protein);
> i=location:location+length(motif)-1; %cut the protein vector into the
> %same length as the motif vector given the input location
> if rows~=1
> disp('motif_match only accepts vectors') %Error checking; proteins can not have multiple rows.
> elseif motif==protein(i) %protein of length of motif, starting at designated location, must be the
> %same as motif to be true.
> a=1;
> else a=0; %if the aforementioned conditions do not apply, then the statement is false
> end
>
> Part II
> function loc=Motif_Find(motif,protein)
> [rows cols]=size(protein);
> if rows~=1
> disp('motif_match only accepts vectors') %Error checking; proteins can not have multiple rows.
> loc=[];
> else
> loc=1;
> i=1;
> while i<=length(protein)-3;
> loc=Motif_Match(motif,protein,loc)*i;
> i=i+1;
> end
> end

So what is your specific question for this homework assignment in a programming course that "misuses" Matlab ...

Jos

Subject: Function: location

From: Steven Lord

Date: 10 Nov, 2009 15:01:57

Message: 3 of 3


"Justin " <riley127099@yahoo.com> wrote in message
news:hdat28$6qk$1@fred.mathworks.com...
> Objective: Trying to write a function that pinpoints the location at which
> a smaller chain of letters "ex. 'AB' " appears in a longer chain of
> letters "ex. 'BCDCABDEDAB' " and therefore, the function would return 5
> and 10. Here's my coding for the function.
> Any of matlab's built-in functions are not allowed. Only explicit looping
> can be used.

You cannot complete the assignment with these restrictions, as I believe I
said before (although it could have been in response to a different poster.)

> PART I
> function a=Motif_Match(motif,protein,location)
> [rows cols]=size(protein);

You've already broken the restriction. The SIZE function is a built-in
function and so cannot be used, by the terms of the assignment.

> i=location:location+length(motif)-1; %cut the protein vector into the

COLON ( : ), LENGTH, PLUS and MINUS are also all built-in functions.

> %same length as the motif vector given the input location
> if rows~=1

NE (for Not Equals, or ~=) is also built-in.

> disp('motif_match only accepts vectors') %Error checking; proteins can
> not have multiple rows.

DISP.

> elseif motif==protein(i) %protein of length of motif, starting at
> designated location, must be the

EQ (==), and if you want to get technical SUBSREF for the subscripted
referencing.

> %same as motif to be true.
> a=1;
> else a=0; %if the aforementioned conditions do not apply, then the
> statement is false
> end

IF, ELSEIF, and ELSE are keywords, so I guess they're okay, and assignment
isn't really a built-in function ... but other than that you've used plenty
of built-in functions in your code above.

If Justin's teacher/professor is reading -- you've given your student an
impossible task.

Assuming you're only prohibited from using the STRFIND built-in function and
similar string-manipulation functions (with which this assignment would be a
two-liner) your code above is pretty close. I wouldn't use == in this case,
though; there's another function that will tell you if one string IS EQUAL
to another.

Alternatly, if FIND is allowed, there's a small shortcut you can use to
limit the number of locations you have to check.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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
homework Jos (10584) 10 Nov, 2009 02:34:13
rssFeed for this Thread

Contact us at files@mathworks.com