Results 1 to 7 of 7

Math Help - MATLAB - Function M-Files

  1. #1
    Junior Member
    Joined
    Dec 2008
    From
    London
    Posts
    45

    MATLAB - Function M-Files

    Hi guys i'm new to this software package and require some assistance on making 2 function M-files...


    1)Write a function file replace that replaces all elements in a row vector that are between -2 and +2 by 0. The function file should operate on row vectors of arbitrary length. You are not allowed to use the Matlab function find.
    Hints: Use a for loop and an if statement. The length of a row vector x can be
    obtained using the Matlab command length(x).

    2) Write a function file FindPositive that takes in a row vector of arbitrary length and returns the first positive value in the row vector, as well as the index of the row vector where the first positive value occurred. You are not allowed to use a for loop or an if statement in your solution.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Kevlar View Post
    Hi guys i'm new to this software package and require some assistance on making 2 function M-files...


    1)Write a function file replace that replaces all elements in a row vector that are between -2 and +2 by 0. The function file should operate on row vectors of arbitrary length. You are not allowed to use the Matlab function find.
    Hints: Use a for loop and an if statement. The length of a row vector x can be
    obtained using the Matlab command length(x).
    Obeying the rule that find not be used, but not using the hint and probably getting no marks:

    Code:
    function rv=repl(x)
    
      rv=x-((x<2)&&(x>-2)).*x;
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Kevlar View Post
    Hi guys i'm new to this software package and require some assistance on making 2 function M-files...


    1)Write a function file replace that replaces all elements in a row vector that are between -2 and +2 by 0. The function file should operate on row vectors of arbitrary length. You are not allowed to use the Matlab function find.
    Hints: Use a for loop and an if statement. The length of a row vector x can be
    obtained using the Matlab command length(x).

    2) Write a function file FindPositive that takes in a row vector of arbitrary length and returns the first positive value in the row vector, as well as the index of the row vector where the first positive value occurred. You are not allowed to use a for loop or an if statement in your solution.
    The second is easy, using the hint of what you were not allowed to use in part 1 (use find). That is unless you have to guard against there being no positive values?

    CB
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Junior Member
    Joined
    Dec 2008
    From
    London
    Posts
    45
    for the 1st one i was doing somethin along the lines of

    Code:
    function b=replace(a)
    
    %This function replaces all elements in a row or column that are
    %between 2 and -2 with 0 and assisns the new elements to the row b
    
    L=length(a);
       for k=1:L
          if a(k)<2|a(k)>-2
             b(k,1)=0;
          else
             b(k,1)=a(k);
    end
    end
    (Can't check my code till monday now don't have matlab on my pc only on the uni pc's)

    For the second one still not sure as i dunno how to use the find function as of yet >.< !
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Kevlar View Post
    for the 1st one i was doing somethin along the lines of

    Code:
    function b=replace(a)
    
    %This function replaces all elements in a row or column that are
    %between 2 and -2 with 0 and assisns the new elements to the row b
    
    L=length(a);
       for k=1:L
          if a(k)<2|a(k)>-2
             b(k,1)=0;
          else
             b(k,1)=a(k);
    end
    end
    (Can't check my code till monday now don't have matlab on my pc only on the uni pc's)

    For the second one still not sure as i dunno how to use the find function as of yet >.< !
    You can install FreeMat for basic matlab coding jobs, its free and pretty good.

    The or in your code should be an and (and last time I looked the editor wanted me to use || and && rather than | and &, but both seem to still work)

    CB
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Second to CB's post, Octave is also pretty good (and the syntax is ~95% transparent between languages I believe). If you are going to be using matlab for an extended period of time however, the student version is great value for money.

    Elbarto
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by elbarto View Post
    Second to CB's post, Octave is also pretty good (and the syntax is ~95% transparent between languages I believe). If you are going to be using matlab for an extended period of time however, the student version is great value for money.

    Elbarto
    The student version is indeed great value for money, and this is one of the things I dislike about MathWorks.

    A complete distribution is available to students to turn them into Matlab users for life, so when they get jobs they will want their employers to provide Matlab as their tool of choice. Now here is the rub, the Matlab installation on my work machine is by no means complete and it has cost my employer about 10000 to provide + annual maintenance fees (there is a basic price for the Matlab core functionality then ca 600 per additional toolbox and you need a fair number of those tool boxes).

    Educational institutions should not be aiding in this conspiracy.

    Employers with a corporate policy of not using open source software (yes, I'm looking at you RandomTLA-Systems) are also to blame for this situation (assuming their accounting systems give them any indication of how much they are paying MathWorks for those 1000's of Matlab seats)

    (I won't mention the ludicrous amount of time it takes Matlab to boot, when using an alternative I will have solved the kind of small problem that often comes up before Matlab will have booted)

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: February 19th 2011, 05:54 AM
  2. Replies: 8
    Last Post: June 10th 2010, 03:36 AM
  3. Matlab function M-Files
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 5th 2010, 04:33 AM
  4. Play .avi files in MATLAB GUI?
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 24th 2009, 05:06 PM
  5. Matlab, vectors and function files
    Posted in the Math Software Forum
    Replies: 2
    Last Post: August 20th 2009, 01:41 PM

Search Tags


/mathhelpforum @mathhelpforum