3200
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 |
6754
|
23 ## @var{x}. If the elements of @var{x} are sorted, the median is defined |
|
24 ## as |
3367
|
25 ## @iftex |
|
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 |
|
33 ## @end iftex |
|
34 ## @ifinfo |
3426
|
35 ## |
3367
|
36 ## @example |
|
37 ## @group |
|
38 ## x(ceil(N/2)), N odd |
3426
|
39 ## median(x) = |
3367
|
40 ## (x(N/2) + x((N/2)+1))/2, N even |
|
41 ## @end group |
|
42 ## @end example |
|
43 ## @end ifinfo |
|
44 ## If @var{x} is a matrix, compute the median value for each |
6754
|
45 ## column and return them in a row vector. If the optional @var{dim} |
|
46 ## argument is given, operate along this dimension. |
5642
|
47 ## @seealso{std, mean} |
3367
|
48 ## @end deftypefn |
3200
|
49 |
|
50 ## Author: jwe |
|
51 |
4852
|
52 function retval = median (a, dim) |
3200
|
53 |
4852
|
54 if (nargin != 1 && nargin != 2) |
6046
|
55 print_usage (); |
4852
|
56 endif |
|
57 if (nargin < 2) |
6024
|
58 dim = find (size (a) > 1, 1); |
4852
|
59 if (isempty (dim)) |
|
60 dim = 1; |
|
61 endif |
3200
|
62 endif |
|
63 |
4852
|
64 sz = size (a); |
|
65 s = sort (a, dim); |
5123
|
66 if (numel (a) > 0) |
4852
|
67 if (numel (a) == sz(dim)) |
|
68 if (rem (sz(dim), 2) == 0) |
|
69 i = sz(dim) / 2; |
|
70 retval = (s(i) + s(i+1)) / 2; |
|
71 else |
|
72 i = ceil (sz(dim) /2); |
|
73 retval = s(i); |
|
74 endif |
3200
|
75 else |
4852
|
76 idx = cell (); |
|
77 nd = length (sz); |
|
78 for i = 1:nd |
|
79 idx{i} = 1:sz(i); |
|
80 endfor |
|
81 if (rem (sz(dim), 2) == 0) |
|
82 i = sz(dim) / 2; |
|
83 idx{dim} = i; |
|
84 retval = s(idx{:}); |
|
85 idx{dim} = i+1; |
|
86 retval = (retval + s(idx{:})) / 2; |
|
87 else |
|
88 idx{dim} = ceil (sz(dim) / 2); |
|
89 retval = s(idx{:}); |
|
90 endif |
3200
|
91 endif |
|
92 else |
|
93 error ("median: invalid matrix argument"); |
|
94 endif |
|
95 |
|
96 endfunction |