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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11436
diff changeset
1 ## Copyright (C) 1996-2011 John W. Eaton
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11436
diff changeset
2 ## Copyright (C) 2009-2010 VZLU Prague
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
3 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
5 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
9 ## your option) any later version.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
10 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
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
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
25 ## as
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
26 ## @tex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
27 ## $$
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
28 ## {\rm median} (x) =
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
29 ## \cases{x(\lceil N/2\rceil), & $N$ odd;\cr
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
30 ## (x(N/2)+x(N/2+1))/2, & $N$ even.}
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
31 ## $$
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
34 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
35 ## @example
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
36 ## @group
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
37 ## x(ceil(N/2)), N odd
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
38 ## median(x) =
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
39 ## (x(N/2) + x((N/2)+1))/2, N even
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
40 ## @end group
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
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
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
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
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
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
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
48 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
49
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
50 ## Author: jwe
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
53
4852
404c7122853e [project @ 2004-04-06 17:23:37 by jwe]
jwe
parents: 3426
diff changeset
54 if (nargin != 1 && nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
55 print_usage ();
4852
404c7122853e [project @ 2004-04-06 17:23:37 by jwe]
jwe
parents: 3426
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
60 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
87 else
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
88 error ("median: invalid matrix argument");
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
89 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
90
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
91 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
92
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
93 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
94 %! x = [1, 2, 3, 4, 5, 6];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
95 %! x2 = x';
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
96 %! y = [1, 2, 3, 4, 5, 6, 7];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
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
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
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
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
107 %!error median ();
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
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
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
114