Mercurial > hg > octave-nkf
annotate scripts/statistics/base/median.m @ 12575:d0b799dafede
Grammarcheck files for 3.4.1 release.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 04 Apr 2011 15:33:46 -0700 |
parents | c792872f8942 |
children | 6b2f14af2360 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 John W. Eaton |
2 ## Copyright (C) 2009-2010 VZLU Prague | |
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 -*- |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10655
diff
changeset
|
21 ## @deftypefn {Function File} {} median (@var{x}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10655
diff
changeset
|
22 ## @deftypefnx {Function File} {} median (@var{x}, @var{dim}) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
23 ## Compute the median value of the elements of the vector @var{x}. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
24 ## If the elements of @var{x} are sorted, the median is defined |
6754 | 25 ## as |
3367 | 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 | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
33 ## @ifnottex |
3426 | 34 ## |
3367 | 35 ## @example |
36 ## @group | |
37 ## x(ceil(N/2)), N odd | |
3426 | 38 ## median(x) = |
3367 | 39 ## (x(N/2) + x((N/2)+1))/2, N even |
40 ## @end group | |
41 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
42 ## |
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 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
45 ## column and return them in a row vector. If the optional @var{dim} |
6754 | 46 ## argument is given, operate along this dimension. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
47 ## @seealso{mean, mode} |
3367 | 48 ## @end deftypefn |
3200 | 49 |
50 ## Author: jwe | |
51 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
52 function retval = median (x, dim) |
3200 | 53 |
4852 | 54 if (nargin != 1 && nargin != 2) |
6046 | 55 print_usage (); |
4852 | 56 endif |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
57 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
58 if (!isnumeric (x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
59 error ("median: X must be a numeric vector or matrix"); |
3200 | 60 endif |
61 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
62 nd = ndims (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
63 sz = size (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
64 if (nargin != 2) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
65 ## Find the first non-singleton dimension. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
66 dim = find (sz > 1, 1); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
67 if (isempty (dim)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
68 dim = 1; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
69 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
70 else |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
71 if (!(isscalar (dim) && dim == fix (dim)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
72 || !(1 <= dim && dim <= nd)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
73 error ("median: DIM must be an integer and a valid dimension"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
74 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
75 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
76 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
77 if (numel (x) > 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
78 n = size (x, dim); |
9726
b7b89061bd0e
reimplement median using nth_element
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
79 k = floor ((n+1) / 2); |
b7b89061bd0e
reimplement median using nth_element
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
80 if (mod (n, 2) == 1) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
81 retval = nth_element (x, k, dim); |
3200 | 82 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
83 retval = mean (nth_element (x, k:k+1, dim), dim); |
3200 | 84 endif |
10655
9e4407c19e8d
make median more Matlab compatible (bug #29930)
Jaroslav Hajek <highegg@gmail.com>
parents:
9726
diff
changeset
|
85 ## Inject NaNs where needed, to be consistent with Matlab. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
86 retval(any (isnan (x), dim)) = NaN; |
3200 | 87 else |
88 error ("median: invalid matrix argument"); | |
89 endif | |
90 | |
91 endfunction | |
7411 | 92 |
93 %!test | |
94 %! x = [1, 2, 3, 4, 5, 6]; | |
95 %! x2 = x'; | |
96 %! y = [1, 2, 3, 4, 5, 6, 7]; | |
97 %! y2 = y'; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
98 %! |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
99 %! assert(median (x) == median (x2) && median (x) == 3.5); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
100 %! assert(median (y) == median (y2) && median (y) == 4); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
101 %! assert(median ([x2, 2*x2]) == [3.5, 7]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
102 %! assert(median ([y2, 3*y2]) == [4, 12]); |
7411 | 103 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
104 %!assert(median ([1,2,NaN;4,5,6;NaN,8,9]), [NaN, 5, NaN]); |
11360
2b03258c240b
median.m: Add NaN test case from bug #29930.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
105 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
106 %% Test input validation |
7411 | 107 %!error median (); |
108 %!error median (1, 2, 3); | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
109 %!error median ({1:5}); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
110 %!error median (true(1,5)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
111 %!error median (1, ones(2,2)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
112 %!error median (1, 1.5); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
113 %!error median (1, 0); |
7411 | 114 |