Mercurial > hg > octave-nkf
annotate scripts/statistics/base/median.m @ 8517:81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
author | sh@sh-laptop |
---|---|
date | Wed, 14 Jan 2009 20:44:25 -0500 |
parents | 83a8781b529d |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007 |
2 ## John W. Eaton | |
3200 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3200 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3200 | 19 |
3367 | 20 ## -*- texinfo -*- |
6754 | 21 ## @deftypefn {Function File} {} median (@var{x}, @var{dim}) |
3367 | 22 ## If @var{x} is a vector, compute the median value of the elements of |
6754 | 23 ## @var{x}. If the elements of @var{x} are sorted, the median is defined |
24 ## as | |
3367 | 25 ## @iftex |
26 ## @tex | |
27 ## $$ | |
28 ## {\rm median} (x) = | |
29 ## \cases{x(\lceil N/2\rceil), & $N$ odd;\cr | |
30 ## (x(N/2)+x(N/2+1))/2, & $N$ even.} | |
31 ## $$ | |
32 ## @end tex | |
33 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
34 ## @ifnottex |
3426 | 35 ## |
3367 | 36 ## @example |
37 ## @group | |
38 ## x(ceil(N/2)), N odd | |
3426 | 39 ## median(x) = |
3367 | 40 ## (x(N/2) + x((N/2)+1))/2, N even |
41 ## @end group | |
42 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
43 ## @end ifnottex |
3367 | 44 ## If @var{x} is a matrix, compute the median value for each |
6754 | 45 ## column and return them in a row vector. If the optional @var{dim} |
46 ## argument is given, operate along this dimension. | |
5642 | 47 ## @seealso{std, mean} |
3367 | 48 ## @end deftypefn |
3200 | 49 |
50 ## Author: jwe | |
51 | |
4852 | 52 function retval = median (a, dim) |
3200 | 53 |
4852 | 54 if (nargin != 1 && nargin != 2) |
6046 | 55 print_usage (); |
4852 | 56 endif |
57 if (nargin < 2) | |
6024 | 58 dim = find (size (a) > 1, 1); |
4852 | 59 if (isempty (dim)) |
60 dim = 1; | |
61 endif | |
3200 | 62 endif |
63 | |
4852 | 64 sz = size (a); |
65 s = sort (a, dim); | |
5123 | 66 if (numel (a) > 0) |
4852 | 67 if (numel (a) == sz(dim)) |
68 if (rem (sz(dim), 2) == 0) | |
69 i = sz(dim) / 2; | |
70 retval = (s(i) + s(i+1)) / 2; | |
71 else | |
72 i = ceil (sz(dim) /2); | |
73 retval = s(i); | |
74 endif | |
3200 | 75 else |
4852 | 76 idx = cell (); |
77 nd = length (sz); | |
78 for i = 1:nd | |
79 idx{i} = 1:sz(i); | |
80 endfor | |
81 if (rem (sz(dim), 2) == 0) | |
82 i = sz(dim) / 2; | |
83 idx{dim} = i; | |
84 retval = s(idx{:}); | |
85 idx{dim} = i+1; | |
86 retval = (retval + s(idx{:})) / 2; | |
87 else | |
88 idx{dim} = ceil (sz(dim) / 2); | |
89 retval = s(idx{:}); | |
90 endif | |
3200 | 91 endif |
92 else | |
93 error ("median: invalid matrix argument"); | |
94 endif | |
95 | |
96 endfunction | |
7411 | 97 |
98 %!test | |
99 %! x = [1, 2, 3, 4, 5, 6]; | |
100 %! x2 = x'; | |
101 %! y = [1, 2, 3, 4, 5, 6, 7]; | |
102 %! y2 = y'; | |
103 %! | |
104 %! assert((median (x) == median (x2) && median (x) == 3.5 | |
105 %! && median (y) == median (y2) && median (y) == 4 | |
106 %! && median ([x2, 2*x2]) == [3.5, 7] | |
107 %! && median ([y2, 3*y2]) == [4, 12])); | |
108 | |
109 %!error median (); | |
110 | |
111 %!error median (1, 2, 3); | |
112 |