Mercurial > hg > octave-nkf
annotate scripts/statistics/base/median.m @ 14570:d07d96e53612 stable
seconds after the minute can be 0-60, not 0-61
* system.txi (Timing Utilities): Correct possible values for number of
seconds in time structures. From Rafael Arndt <rafaelarndt@gmail.com>.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 17 Apr 2012 14:42:49 -0400 |
parents | 3d4bea9accd7 |
children | 12ccdce2c216 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12656
diff
changeset
|
1 ## Copyright (C) 1996-2012 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 -*- |
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 | |
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 |
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 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
58 if (! (isnumeric (x) || islogical (x))) |
11436
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 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 if (isempty (x)) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
63 error ("median: X cannot be an empty matrix"); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
64 endif |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
65 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
66 nd = ndims (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
67 sz = size (x); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
68 if (nargin < 2) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
69 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
70 (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
|
71 else |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
72 if (!(isscalar (dim) && dim == fix (dim)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
73 || !(1 <= dim && dim <= nd)) |
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 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
78 n = sz(dim); |
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 | 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 | 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 | 87 |
88 endfunction | |
7411 | 89 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
90 |
7411 | 91 %!test |
92 %! x = [1, 2, 3, 4, 5, 6]; | |
93 %! x2 = x'; | |
94 %! y = [1, 2, 3, 4, 5, 6, 7]; | |
95 %! y2 = y'; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
96 %! |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
97 %! 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
|
98 %! 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
|
99 %! 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
|
100 %! assert(median ([y2, 3*y2]) == [4, 12]); |
7411 | 101 |
14440
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
102 %% Test multidimensional arrays (bug #35679) |
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
103 %!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
|
104 %! rand ("seed", 2); |
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
105 %! 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
|
106 %! 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
|
107 %! x = sort (a, 4); |
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
108 %! y = sort (b, 3); |
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14327
diff
changeset
|
109 %!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
|
110 %!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
|
111 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
112 %!assert(median (single([1,2,3])), single(2)); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
113 %!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
|
114 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
115 %% Test input validation |
7411 | 116 %!error median (); |
117 %!error median (1, 2, 3); | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
118 %!error median ({1:5}); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
119 %!error median (['A'; 'B']); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
120 %!error median (1, ones(2,2)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
121 %!error median (1, 1.5); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
122 %!error median (1, 0); |
7411 | 123 |