Results 1 to 9 of 9

Math Help - Creating Matlab Median Function

  1. #1
    Newbie
    Joined
    Oct 2009
    Posts
    2

    Creating Matlab Median Function

    I first entered the code
    function k=mymedian(p)
    Now I have to sort the vecotr and find out how to type it out so that I can receive the median of a vector.
    Does anybody understand this problem?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    do you mean:
    Code:
    k=mymedian(sort(p))
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    you may also be interested in this if you dont want to use the standard matlab "sort" function.

    http://www.mathhelpforum.com/math-he...or-matlab.html

    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Oct 2009
    Posts
    2
    Thanks for the help I solved it by using the following code
    function k=mymedian(p)
    w=sort(p);
    r=length(w);
    g=floor(r/2);
    k=w(g+1);
    if(g*2==r)
    k=(w(r)+ k)/2;
    end

    Does anybody have ideas on how to do the same concept for a matrice?
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    change
    Code:
    w=sort(p);
    to
    Code:
    w=sort(p(:));
    Code is untested but should work.


    Elbarto
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by elbarto View Post
    change
    Code:
    w=sort(p);
    to
    Code:
    w=sort(p(:));
    Code is untested but should work.


    Elbarto
    Code:
    w=sort(p);
    will work just fine.

    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Quote Originally Posted by CaptainBlack View Post
    Code:
    w=sort(p);
    will work just fine.

    CB
    This will return a sorted matrix for "w". The orginal code uses the length function so not all values will be included will they?

    @OP, when finding the median of the matrix, do you only want the median of the entire matrix or the median of each column like matlab calculates?

    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by elbarto View Post
    This will return a sorted matrix for "w". The orginal code uses the length function so not all values will be included will they?

    @OP, when finding the median of the matrix, do you only want the median of the entire matrix or the median of each column like matlab calculates?

    Regards Elbarto
    The op specifies vector repeatedly. If fed a matrix it sorts the columns.

    CB
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    I tried the code in #4 and got the following output:

    Code:
    DU>> A = magic(5)
    
    A =
    
        17    24     1     8    15
        23     5     7    14    16
         4     6    13    20    22
        10    12    19    21     3
        11    18    25     2     9
    
    EDU>> mymedian(A)
    
    ans =
    
        11
    
    EDU>>
    It is only returning the median of the first column for me?
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Creating images in Matlab
    Posted in the Math Software Forum
    Replies: 3
    Last Post: June 25th 2011, 06:45 AM
  2. Help creating a function diagram
    Posted in the Discrete Math Forum
    Replies: 4
    Last Post: May 15th 2010, 12:22 PM
  3. Creating animated plot of vectors in Matlab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 4th 2010, 12:33 AM
  4. Matlab Help: Creating M-File
    Posted in the Math Software Forum
    Replies: 2
    Last Post: March 24th 2010, 08:12 PM
  5. Creating a loop in Matlab help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 11th 2010, 03:06 AM

Search Tags


/mathhelpforum @mathhelpforum