Mercurial > hg > octave-nkf
annotate scripts/statistics/base/median.m @ 10821:693e22af08ae
Grammarcheck documentation of m-files
Add newlines between @item fields for readability.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 26 Jul 2010 21:25:36 -0700 |
parents | a8ce6bdecce5 |
children | 2b03258c240b |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007, |
2 ## 2008, 2009 John W. Eaton | |
10655
9e4407c19e8d
make median more Matlab compatible (bug #29930)
Jaroslav Hajek <highegg@gmail.com>
parents:
9726
diff
changeset
|
3 ## Copyright (C) 2009, 2010 VZLU Prague |
3200 | 4 ## |
5 ## This file is part of Octave. | |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
3200 | 11 ## |
12 ## Octave is distributed in the hope that it will be useful, but | |
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 ## General Public License for more details. | |
16 ## | |
17 ## You should have received a copy of the GNU General Public License | |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
3200 | 20 |
3367 | 21 ## -*- texinfo -*- |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10655
diff
changeset
|
22 ## @deftypefn {Function File} {} median (@var{x}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10655
diff
changeset
|
23 ## @deftypefnx {Function File} {} median (@var{x}, @var{dim}) |
3367 | 24 ## If @var{x} is a vector, compute the median value of the elements of |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## @var{x}. If the elements of @var{x} are sorted, the median is defined |
6754 | 26 ## as |
3367 | 27 ## @tex |
28 ## $$ | |
29 ## {\rm median} (x) = | |
30 ## \cases{x(\lceil N/2\rceil), & $N$ odd;\cr | |
31 ## (x(N/2)+x(N/2+1))/2, & $N$ even.} | |
32 ## $$ | |
33 ## @end tex | |
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 | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
43 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
44 ## @end ifnottex |
3367 | 45 ## If @var{x} is a matrix, compute the median value for each |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
46 ## column and return them in a row vector. If the optional @var{dim} |
6754 | 47 ## argument is given, operate along this dimension. |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10655
diff
changeset
|
48 ## @seealso{mean,mode} |
3367 | 49 ## @end deftypefn |
3200 | 50 |
51 ## Author: jwe | |
52 | |
4852 | 53 function retval = median (a, dim) |
3200 | 54 |
4852 | 55 if (nargin != 1 && nargin != 2) |
6046 | 56 print_usage (); |
4852 | 57 endif |
58 if (nargin < 2) | |
10655
9e4407c19e8d
make median more Matlab compatible (bug #29930)
Jaroslav Hajek <highegg@gmail.com>
parents:
9726
diff
changeset
|
59 [~, dim] = max (size (a) != 1); # First non-singleton dim. |
3200 | 60 endif |
61 | |
5123 | 62 if (numel (a) > 0) |
9726
b7b89061bd0e
reimplement median using nth_element
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
63 n = size (a, dim); |
b7b89061bd0e
reimplement median using nth_element
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
64 k = floor ((n+1) / 2); |
b7b89061bd0e
reimplement median using nth_element
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
65 if (mod (n, 2) == 1) |
b7b89061bd0e
reimplement median using nth_element
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
66 retval = nth_element (a, k, dim); |
3200 | 67 else |
9726
b7b89061bd0e
reimplement median using nth_element
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
68 retval = mean (nth_element (a, k:k+1, dim), dim); |
3200 | 69 endif |
10655
9e4407c19e8d
make median more Matlab compatible (bug #29930)
Jaroslav Hajek <highegg@gmail.com>
parents:
9726
diff
changeset
|
70 ## Inject NaNs where needed, to be consistent with Matlab. |
9e4407c19e8d
make median more Matlab compatible (bug #29930)
Jaroslav Hajek <highegg@gmail.com>
parents:
9726
diff
changeset
|
71 retval(any (isnan (a), dim)) = NaN; |
3200 | 72 else |
73 error ("median: invalid matrix argument"); | |
74 endif | |
75 | |
76 endfunction | |
7411 | 77 |
78 %!test | |
79 %! x = [1, 2, 3, 4, 5, 6]; | |
80 %! x2 = x'; | |
81 %! y = [1, 2, 3, 4, 5, 6, 7]; | |
82 %! y2 = y'; | |
83 %! | |
84 %! assert((median (x) == median (x2) && median (x) == 3.5 | |
85 %! && median (y) == median (y2) && median (y) == 4 | |
86 %! && median ([x2, 2*x2]) == [3.5, 7] | |
87 %! && median ([y2, 3*y2]) == [4, 12])); | |
88 | |
89 %!error median (); | |
90 | |
91 %!error median (1, 2, 3); | |
92 |