Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old October 26th, 2009, 10:39 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
Country:
Thanks: 0
Thanked 0 Times in 0 Posts
sum1 is on a distinguished road
Default 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?
Reply With Quote
Advertisement
 
  #2  
Old October 26th, 2009, 10:50 PM
Member
 
Join Date: Mar 2007
Posts: 141
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

do you mean:
Code:
k=mymedian(sort(p))
Reply With Quote
  #3  
Old October 26th, 2009, 11:29 PM
Member
 
Join Date: Mar 2007
Posts: 141
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

you may also be interested in this if you dont want to use the standard matlab "sort" function.

Re-ordering a vector... (MATLAB)

Regards Elbarto
Reply With Quote
  #4  
Old October 26th, 2009, 11:52 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
Country:
Thanks: 0
Thanked 0 Times in 0 Posts
sum1 is on a distinguished road
Default

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?
Reply With Quote
  #5  
Old October 27th, 2009, 12:36 AM
Member
 
Join Date: Mar 2007
Posts: 141
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

change
Code:
w=sort(p);
to
Code:
w=sort(p(:));
Code is untested but should work.


Elbarto
Reply With Quote
  #6  
Old October 27th, 2009, 01:17 AM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 12,190
Country:
Thanks: 770
Thanked 3,953 Times in 3,191 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

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
__________________
Truth does not change because it is, or is not, believed by a majority of the people.

Giordano Bruno
Reply With Quote
  #7  
Old October 27th, 2009, 01:54 AM
Member
 
Join Date: Mar 2007
Posts: 141
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
  #8  
Old October 27th, 2009, 10:48 AM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 12,190
Country:
Thanks: 770
Thanked 3,953 Times in 3,191 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

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
__________________
Truth does not change because it is, or is not, believed by a majority of the people.

Giordano Bruno
Reply With Quote
  #9  
Old October 27th, 2009, 05:22 PM
Member
 
Join Date: Mar 2007
Posts: 141
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

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?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 03:51 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.