annotate scripts/statistics/base/median.m @ 21309:7fbecef105ca

Allow statistics functions to work over non-existent dimension (bug #33523). * var.m, center.m, kurtosis.m, mean.m, meansq.m, median.m, moment.m, skewness.m, std.m: Remove check for input dimension being within the range of ndims (x). Add BIST tests for new behavior and update input validation tests.
author Rik <rik@octave.org>
date Fri, 19 Feb 2016 21:27:03 -0800
parents 516bb87ea72e
children a4faec57f4c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 1996-2015 John W. Eaton
11523
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 -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
21 ## @deftypefn {} {} median (@var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
22 ## @deftypefnx {} {} 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}.
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
24 ##
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
25 ## When the elements of @var{x} are sorted, the median is defined 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
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
37 ## x(ceil(N/2)) N odd
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
38 ## median (x) =
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
39 ## (x(N/2) + x((N/2)+1))/2 N even
3367
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
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ## If @var{x} is a matrix, compute the median value for each column and
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## return them in a row vector.
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ##
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ## If the optional @var{dim} 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
48 ## @seealso{mean, mode}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
49 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
50
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
51 ## Author: jwe
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
52
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
53 function retval = median (x, dim)
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
54
4852
404c7122853e [project @ 2004-04-06 17:23:37 by jwe]
jwe
parents: 3426
diff changeset
55 if (nargin != 1 && nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
56 print_usage ();
4852
404c7122853e [project @ 2004-04-06 17:23:37 by jwe]
jwe
parents: 3426
diff changeset
57 endif
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
58
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
59 if (! (isnumeric (x) || islogical (x)))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
60 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
61 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
62
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
63 if (isempty (x))
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
64 error ("median: X cannot be an empty matrix");
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
65 endif
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
66
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
67 nd = ndims (x);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
68 sz = size (x);
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
69 if (nargin < 2)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
70 ## Find the first non-singleton dimension.
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
71 (dim = find (sz > 1, 1)) || (dim = 1);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
72 else
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
73 if (! (isscalar (dim) && dim == fix (dim) && dim > 0))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
74 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
75 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
76 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11360
diff changeset
77
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
78 n = size (x, dim);
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
79 k = floor ((n+1) / 2);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
80 if (mod (n, 2) == 1)
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
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
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
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
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
85 ## Inject NaNs where needed, to be consistent with Matlab.
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
88 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
89
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
90
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
91 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
92 %! x = [1, 2, 3, 4, 5, 6];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
93 %! x2 = x';
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
94 %! y = [1, 2, 3, 4, 5, 6, 7];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
95 %! y2 = y';
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
96 %!
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
97 %! assert (median (x) == median (x2) && median (x) == 3.5);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
98 %! assert (median (y) == median (y2) && median (y) == 4);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
99 %! assert (median ([x2, 2*x2]), [3.5, 7]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
100 %! assert (median ([y2, 3*y2]), [4, 12]);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
101
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
102 %!assert (median (single ([1,2,3])), single (2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
103 %!assert (median ([1,2,NaN;4,5,6;NaN,8,9]), [NaN, 5, NaN])
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
104 %!assert (median ([1,2], 3), [1,2])
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
105
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
106 ## Test multidimensional arrays (bug #35679)
14440
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
107 %!shared a, b, x, y
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
108 %! rand ("seed", 2);
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
109 %! a = rand (2,3,4,5);
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
110 %! b = rand (3,4,6,5);
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
111 %! x = sort (a, 4);
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
112 %! y = sort (b, 3);
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
113 %!assert (median (a, 4), x(:, :, :, 3));
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
114 %!assert (median (b, 3), (y(:, :, 3, :) + y(:, :, 4, :))/2);
3d4bea9accd7 Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14327
diff changeset
115
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
116 ## Test input validation
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
117 %!error median ()
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
118 %!error median (1, 2, 3)
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
119 %!error <X must be a numeric> median ({1:5})
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
120 %!error <X cannot be an empty matrix> median ([])
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
121 %!error <DIM must be an integer> median (1, ones (2,2))
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
122 %!error <DIM must be an integer> median (1, 1.5)
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
123 %!error <DIM must be .* a valid dimension> median (1, 0)
11360
2b03258c240b median.m: Add NaN test case from bug #29930.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
124