Mercurial > hg > octave-nkf
annotate scripts/statistics/base/median.m @ 14327:4d917a6a858b stable
doc: Use Octave coding conventions in @example blocks of docstrings.
* accumarray.m, accumdim.m, bar.m, base2dec.m, bincoeff.m, bitcmp.m, bitset.m,
celldisp.m, chop.m, clabel.m, cloglog.m, colon.m, compass.m, computer.m,
contour3.m, contourc.m, corr.m, cstrcat.m, ctime.m, cylinder.m, date.m,
dec2base.m, demo.m, dir.m, dlmwrite.m, expm.m, ezcontourf.m, ezcontour.m,
ezmeshc.m, ezmesh.m, ezplot.m, ezsurfc.m, ezsurf.m, feather.m, findobj.m,
flipdim.m, fplot.m, genvarname.m, getfield.m, hankel.m, hilb.m, hist.m,
idivide.m, index.m, int2str.m, interp1.m, is_leap_year.m, ismember.m,
isocolors.m, isonormals.m, isosurface.m, kurtosis.m, legendre.m, linkprop.m,
logit.m, logm.m, __makeinfo__.m, __marching_cube__.m, median.m, mkoctfile.m,
moment.m, mpoles.m, orderfields.m, pcg.m, pcr.m, plot3.m, plotmatrix.m,
polyaffine.m, polygcd.m, poly.m, polyout.m, print.m, qp.m, quadgk.m, qzhess.m,
randi.m, rat.m, refreshdata.m, residue.m, rose.m, rot90.m, saveas.m, saveobj.m,
shiftdim.m, skewness.m, spaugment.m, spdiags.m, sqp.m, stem.m, str2num.m,
strcat.m, strjust.m, strread.m, strsplit.m, structfun.m, subplot.m,
subsindex.m, substruct.m, surfl.m, surfnorm.m, svds.m, uimenu.m, union.m,
voronoi.m, warning_ids.m, wblpdf.m: Use Octave coding conventions in
@example blocks of docstrings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 04 Feb 2012 22:12:50 -0800 |
parents | 72c96de7a403 |
children | f3d52523cde1 3d4bea9accd7 |
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 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
102 %!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
|
103 %!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
|
104 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
105 %% Test input validation |
7411 | 106 %!error median (); |
107 %!error median (1, 2, 3); | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
108 %!error median ({1:5}); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
109 %!error median (['A'; 'B']); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
110 %!error median (1, ones(2,2)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
111 %!error median (1, 1.5); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11360
diff
changeset
|
112 %!error median (1, 0); |
7411 | 113 |