Mercurial > octave
annotate scripts/statistics/base/median.m @ 21540:ffb4770ba079
Make median preserve discrete types (bug #47515).
* median.m: Replace mean by "native" sum (or max for logical), and
avoid assigning NaN to non-float types.
* data.cc (Fnth_element): Allow type bool, treated as an integer.
author | Lachlan Andrew <lachlanbis@gmail.com> |
---|---|
date | Thu, 24 Mar 2016 13:35:40 +1100 |
parents | a4faec57f4c8 |
children | ecce63c99c3f |
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 | 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 -*- |
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 | 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 | |
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 | 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 |
21540
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
44 ## If @var{x} is of a discrete type such as integer or logical, then |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
45 ## the case of even N rounds up (or toward @qcode{true}). |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
46 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
47 ## 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
|
48 ## 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
|
49 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
50 ## 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
|
51 ## @seealso{mean, mode} |
3367 | 52 ## @end deftypefn |
3200 | 53 |
54 ## Author: jwe | |
55 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
56 function retval = median (x, dim) |
3200 | 57 |
4852 | 58 if (nargin != 1 && nargin != 2) |
6046 | 59 print_usage (); |
4852 | 60 endif |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
61 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
63 error ("median: X must be a numeric vector or matrix"); |
3200 | 64 endif |
65 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
66 if (isempty (x)) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
67 error ("median: X cannot be an empty matrix"); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
68 endif |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
69 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
70 nd = ndims (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
71 sz = size (x); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
72 if (nargin < 2) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
73 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
74 (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
|
75 else |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
76 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
|
77 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
|
78 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
79 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
80 |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
81 n = size (x, dim); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
82 k = floor ((n+1) / 2); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
83 if (mod (n, 2) == 1) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
84 retval = nth_element (x, k, dim); |
3200 | 85 else |
21540
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
86 retval = sum (nth_element (x, k:k+1, dim), dim, "native") / 2; |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
87 if (islogical (x)) |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
88 retval = logical (retval); |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
89 endif |
3200 | 90 endif |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
91 ## Inject NaNs where needed, to be consistent with Matlab. |
21540
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
92 if (isfloat (x)) |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
93 retval(any (isnan (x), dim)) = NaN; |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
94 endif |
3200 | 95 |
96 endfunction | |
7411 | 97 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
98 |
7411 | 99 %!test |
100 %! x = [1, 2, 3, 4, 5, 6]; | |
101 %! x2 = x'; | |
102 %! y = [1, 2, 3, 4, 5, 6, 7]; | |
103 %! y2 = y'; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
104 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
105 %! 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
|
106 %! 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
|
107 %! 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
|
108 %! assert (median ([y2, 3*y2]), [4, 12]); |
7411 | 109 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
110 %!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
|
111 %!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
|
112 %!assert (median ([1,2], 3), [1,2]) |
7411 | 113 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
114 ## 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
|
115 %!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
|
116 %! rand ("seed", 2); |
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
117 %! 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
|
118 %! 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
|
119 %! x = sort (a, 4); |
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
120 %! y = sort (b, 3); |
21317
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
21309
diff
changeset
|
121 %!assert (median (a, 4), x(:, :, :, 3)) |
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
21309
diff
changeset
|
122 %!assert (median (b, 3), (y(:, :, 3, :) + y(:, :, 4, :))/2) |
14440
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
123 |
21540
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
124 ## Test non-floating point types |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
125 %!assert (median ([true, false]), true); |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
126 %!assert (median (uint8 ([1, 3])), uint8 (2)); |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
127 %!assert (median (int8 ([1, 3, 4])), int8 (3)); |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
128 %!assert (median (single ([1, 3, 4])), single (3)); |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
129 %!assert (median (single ([1, 3, NaN])), single (NaN)); |
ffb4770ba079
Make median preserve discrete types (bug #47515).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21317
diff
changeset
|
130 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
131 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
132 %!error median () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
133 %!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
|
134 %!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
|
135 %!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
|
136 %!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
|
137 %!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
|
138 %!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
|
139 |