comparison scripts/statistics/base/median.m @ 11436:e151e23f73bc

Overhaul base statistics functions and documentation of same. Add or improve input validation. Add input validation tests. Add functional tests. Improve or re-write documentation strings.
author Rik <octave@nomad.inbox5.com>
date Mon, 03 Jan 2011 21:23:08 -0800
parents 2b03258c240b
children fd0a3ac60b0e
comparison
equal deleted inserted replaced
11435:20f53b3a558f 11436:e151e23f73bc
19 ## <http://www.gnu.org/licenses/>. 19 ## <http://www.gnu.org/licenses/>.
20 20
21 ## -*- texinfo -*- 21 ## -*- texinfo -*-
22 ## @deftypefn {Function File} {} median (@var{x}) 22 ## @deftypefn {Function File} {} median (@var{x})
23 ## @deftypefnx {Function File} {} median (@var{x}, @var{dim}) 23 ## @deftypefnx {Function File} {} median (@var{x}, @var{dim})
24 ## If @var{x} is a vector, compute the median value of the elements of 24 ## Compute the median value of the elements of the vector @var{x}.
25 ## @var{x}. If the elements of @var{x} are sorted, the median is defined 25 ## If the elements of @var{x} are sorted, the median is defined
26 ## as 26 ## as
27 ## @tex 27 ## @tex
28 ## $$ 28 ## $$
29 ## {\rm median} (x) = 29 ## {\rm median} (x) =
30 ## \cases{x(\lceil N/2\rceil), & $N$ odd;\cr 30 ## \cases{x(\lceil N/2\rceil), & $N$ odd;\cr
48 ## @seealso{mean,mode} 48 ## @seealso{mean,mode}
49 ## @end deftypefn 49 ## @end deftypefn
50 50
51 ## Author: jwe 51 ## Author: jwe
52 52
53 function retval = median (a, dim) 53 function retval = median (x, dim)
54 54
55 if (nargin != 1 && nargin != 2) 55 if (nargin != 1 && nargin != 2)
56 print_usage (); 56 print_usage ();
57 endif 57 endif
58 if (nargin < 2) 58
59 [~, dim] = max (size (a) != 1); # First non-singleton dim. 59 if (!isnumeric (x))
60 error ("median: X must be a numeric vector or matrix");
60 endif 61 endif
61 62
62 if (numel (a) > 0) 63 nd = ndims (x);
63 n = size (a, dim); 64 sz = size (x);
65 if (nargin != 2)
66 ## Find the first non-singleton dimension.
67 dim = find (sz > 1, 1);
68 if (isempty (dim))
69 dim = 1;
70 endif
71 else
72 if (!(isscalar (dim) && dim == fix (dim))
73 || !(1 <= dim && dim <= nd))
74 error ("median: DIM must be an integer and a valid dimension");
75 endif
76 endif
77
78 if (numel (x) > 0)
79 n = size (x, dim);
64 k = floor ((n+1) / 2); 80 k = floor ((n+1) / 2);
65 if (mod (n, 2) == 1) 81 if (mod (n, 2) == 1)
66 retval = nth_element (a, k, dim); 82 retval = nth_element (x, k, dim);
67 else 83 else
68 retval = mean (nth_element (a, k:k+1, dim), dim); 84 retval = mean (nth_element (x, k:k+1, dim), dim);
69 endif 85 endif
70 ## Inject NaNs where needed, to be consistent with Matlab. 86 ## Inject NaNs where needed, to be consistent with Matlab.
71 retval(any (isnan (a), dim)) = NaN; 87 retval(any (isnan (x), dim)) = NaN;
72 else 88 else
73 error ("median: invalid matrix argument"); 89 error ("median: invalid matrix argument");
74 endif 90 endif
75 91
76 endfunction 92 endfunction
79 %! x = [1, 2, 3, 4, 5, 6]; 95 %! x = [1, 2, 3, 4, 5, 6];
80 %! x2 = x'; 96 %! x2 = x';
81 %! y = [1, 2, 3, 4, 5, 6, 7]; 97 %! y = [1, 2, 3, 4, 5, 6, 7];
82 %! y2 = y'; 98 %! y2 = y';
83 %! 99 %!
84 %! assert((median (x) == median (x2) && median (x) == 3.5 100 %! assert(median (x) == median (x2) && median (x) == 3.5);
85 %! && median (y) == median (y2) && median (y) == 4 101 %! assert(median (y) == median (y2) && median (y) == 4);
86 %! && median ([x2, 2*x2]) == [3.5, 7] 102 %! assert(median ([x2, 2*x2]) == [3.5, 7]);
87 %! && median ([y2, 3*y2]) == [4, 12])); 103 %! assert(median ([y2, 3*y2]) == [4, 12]);
88 104
89 %!assert (median ([1, 2, 3, NaN]), NaN) 105 %!assert(median ([1,2,NaN;4,5,6;NaN,8,9]), [NaN, 5, NaN]);
90 106
107 %% Test input validation
91 %!error median (); 108 %!error median ();
92 %!error median (1, 2, 3); 109 %!error median (1, 2, 3);
110 %!error median ({1:5});
111 %!error median (true(1,5));
112 %!error median (1, ones(2,2));
113 %!error median (1, 1.5);
114 %!error median (1, 0);
93 115