Mercurial > hg > octave-lyh
annotate scripts/statistics/base/median.m @ 9211:f0c3d3fc4903
Simplify Texinfo documentation in .m scripts by removing redundant @iftex calls
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 17 May 2009 14:39:39 -0700 |
parents | 1bf0ce0930be |
children | b7b89061bd0e |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007, |
2 ## 2008, 2009 John W. Eaton | |
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 -*- |
6754 | 21 ## @deftypefn {Function File} {} median (@var{x}, @var{dim}) |
3367 | 22 ## If @var{x} is a vector, compute the median value of the elements of |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## @var{x}. If the elements of @var{x} are sorted, the median is defined |
6754 | 24 ## as |
3367 | 25 ## @tex |
26 ## $$ | |
27 ## {\rm median} (x) = | |
28 ## \cases{x(\lceil N/2\rceil), & $N$ odd;\cr | |
29 ## (x(N/2)+x(N/2+1))/2, & $N$ even.} | |
30 ## $$ | |
31 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
32 ## @ifnottex |
3426 | 33 ## |
3367 | 34 ## @example |
35 ## @group | |
36 ## x(ceil(N/2)), N odd | |
3426 | 37 ## median(x) = |
3367 | 38 ## (x(N/2) + x((N/2)+1))/2, N even |
39 ## @end group | |
40 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
41 ## @end ifnottex |
3367 | 42 ## 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
|
43 ## column and return them in a row vector. If the optional @var{dim} |
6754 | 44 ## argument is given, operate along this dimension. |
5642 | 45 ## @seealso{std, mean} |
3367 | 46 ## @end deftypefn |
3200 | 47 |
48 ## Author: jwe | |
49 | |
4852 | 50 function retval = median (a, dim) |
3200 | 51 |
4852 | 52 if (nargin != 1 && nargin != 2) |
6046 | 53 print_usage (); |
4852 | 54 endif |
55 if (nargin < 2) | |
6024 | 56 dim = find (size (a) > 1, 1); |
4852 | 57 if (isempty (dim)) |
58 dim = 1; | |
59 endif | |
3200 | 60 endif |
61 | |
4852 | 62 sz = size (a); |
63 s = sort (a, dim); | |
5123 | 64 if (numel (a) > 0) |
4852 | 65 if (numel (a) == sz(dim)) |
66 if (rem (sz(dim), 2) == 0) | |
67 i = sz(dim) / 2; | |
68 retval = (s(i) + s(i+1)) / 2; | |
69 else | |
70 i = ceil (sz(dim) /2); | |
71 retval = s(i); | |
72 endif | |
3200 | 73 else |
4852 | 74 idx = cell (); |
75 nd = length (sz); | |
76 for i = 1:nd | |
77 idx{i} = 1:sz(i); | |
78 endfor | |
79 if (rem (sz(dim), 2) == 0) | |
80 i = sz(dim) / 2; | |
81 idx{dim} = i; | |
82 retval = s(idx{:}); | |
83 idx{dim} = i+1; | |
84 retval = (retval + s(idx{:})) / 2; | |
85 else | |
86 idx{dim} = ceil (sz(dim) / 2); | |
87 retval = s(idx{:}); | |
88 endif | |
3200 | 89 endif |
90 else | |
91 error ("median: invalid matrix argument"); | |
92 endif | |
93 | |
94 endfunction | |
7411 | 95 |
96 %!test | |
97 %! x = [1, 2, 3, 4, 5, 6]; | |
98 %! x2 = x'; | |
99 %! y = [1, 2, 3, 4, 5, 6, 7]; | |
100 %! y2 = y'; | |
101 %! | |
102 %! assert((median (x) == median (x2) && median (x) == 3.5 | |
103 %! && median (y) == median (y2) && median (y) == 4 | |
104 %! && median ([x2, 2*x2]) == [3.5, 7] | |
105 %! && median ([y2, 3*y2]) == [4, 12])); | |
106 | |
107 %!error median (); | |
108 | |
109 %!error median (1, 2, 3); | |
110 |