| 
October 26th, 2009, 10:39 PM
| | Newbie | | Join Date: Oct 2009
Posts: 2
Country: Thanks: 0
Thanked 0 Times in 0 Posts
| | 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? | 
October 26th, 2009, 10:50 PM
| | Member | | Join Date: Mar 2007
Posts: 141
Country: Thanks: 20
Thanked 21 Times in 20 Posts
| | do you mean: Code: k=mymedian(sort(p))
| 
October 26th, 2009, 11:29 PM
| | Member | | Join Date: Mar 2007
Posts: 141
Country: Thanks: 20
Thanked 21 Times in 20 Posts
| | 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 | 
October 26th, 2009, 11:52 PM
| | Newbie | | Join Date: Oct 2009
Posts: 2
Country: Thanks: 0
Thanked 0 Times in 0 Posts
| | 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? | 
October 27th, 2009, 12:36 AM
| | Member | | Join Date: Mar 2007
Posts: 141
Country: Thanks: 20
Thanked 21 Times in 20 Posts
| | change to Code is untested but should work.
Elbarto | 
October 27th, 2009, 01:17 AM
|  | Grand Panjandrum | | Join Date: Nov 2005 Location: South of England
Posts: 12,190
Country: Thanks: 770
Thanked 3,953 Times in 3,191 Posts
| | Quote:
Originally Posted by elbarto change to Code is untested but should work.
Elbarto | will work just fine.
CB
__________________ Truth does not change because it is, or is not, believed by a majority of the people.
Giordano Bruno | 
October 27th, 2009, 01:54 AM
| | Member | | Join Date: Mar 2007
Posts: 141
Country: Thanks: 20
Thanked 21 Times in 20 Posts
| | Quote:
Originally Posted by CaptainBlack | 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 | 
October 27th, 2009, 10:48 AM
|  | Grand Panjandrum | | Join Date: Nov 2005 Location: South of England
Posts: 12,190
Country: Thanks: 770
Thanked 3,953 Times in 3,191 Posts
| | Quote:
Originally Posted by elbarto 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 | 
October 27th, 2009, 05:22 PM
| | Member | | Join Date: Mar 2007
Posts: 141
Country: Thanks: 20
Thanked 21 Times in 20 Posts
| | 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? | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -7. The time now is 03:51 PM. | | |
 | |  |