Mercurial > hg > octave-nkf
comparison scripts/statistics/base/prctile.m @ 18204:b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
* prctile.m, quantile.m: Use first non-singleton dimension when DIM is
unspecified. Change documentation to match new behavior. Add %!tests
to validate change.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 03 Jan 2014 11:58:21 -0800 |
parents | d63878346099 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
18203:adbbacce8aaf | 18204:b29beed0e98d |
---|---|
28 ## return them in a matrix, such that the i-th row of @var{y} contains the | 28 ## return them in a matrix, such that the i-th row of @var{y} contains the |
29 ## @var{p}(i)th percentiles of each column of @var{x}. | 29 ## @var{p}(i)th percentiles of each column of @var{x}. |
30 ## | 30 ## |
31 ## If @var{p} is unspecified, return the quantiles for @code{[0 25 50 75 100]}. | 31 ## If @var{p} is unspecified, return the quantiles for @code{[0 25 50 75 100]}. |
32 ## The optional argument @var{dim} determines the dimension along which | 32 ## The optional argument @var{dim} determines the dimension along which |
33 ## the percentiles are calculated. If @var{dim} is omitted, and @var{x} is | 33 ## the percentiles are calculated. If @var{dim} is omitted it defaults to the |
34 ## a vector or matrix, it defaults to 1 (column-wise quantiles). When | 34 ## the first non-singleton dimension. |
35 ## @var{x} is an N-D array, @var{dim} defaults to the first non-singleton | |
36 ## dimension. | |
37 ## @seealso{quantile} | 35 ## @seealso{quantile} |
38 ## @end deftypefn | 36 ## @end deftypefn |
39 | 37 |
40 ## Author: Ben Abbott <bpabbott@mac.com> | 38 ## Author: Ben Abbott <bpabbott@mac.com> |
41 ## Description: Matlab style prctile function. | 39 ## Description: Matlab style prctile function. |
57 if (! (isnumeric (p) && isvector (p))) | 55 if (! (isnumeric (p) && isvector (p))) |
58 error ("prctile: P must be a numeric vector"); | 56 error ("prctile: P must be a numeric vector"); |
59 endif | 57 endif |
60 | 58 |
61 nd = ndims (x); | 59 nd = ndims (x); |
60 sz = size (x); | |
62 if (nargin < 3) | 61 if (nargin < 3) |
63 if (nd == 2) | 62 ## Find the first non-singleton dimension. |
64 ## If a matrix or vector, always use 1st dimension. | 63 (dim = find (sz > 1, 1)) || (dim = 1); |
65 dim = 1; | |
66 else | |
67 ## If an N-d array, find the first non-singleton dimension. | |
68 (dim = find (sz > 1, 1)) || (dim = 1); | |
69 endif | |
70 else | 64 else |
71 if (!(isscalar (dim) && dim == fix (dim)) | 65 if (!(isscalar (dim) && dim == fix (dim)) |
72 || !(1 <= dim && dim <= nd)) | 66 || !(1 <= dim && dim <= nd)) |
73 error ("prctile: DIM must be an integer and a valid dimension"); | 67 error ("prctile: DIM must be an integer and a valid dimension"); |
74 endif | 68 endif |
82 endfunction | 76 endfunction |
83 | 77 |
84 | 78 |
85 %!test | 79 %!test |
86 %! pct = 50; | 80 %! pct = 50; |
81 %! q = prctile (1:4, pct); | |
82 %! qa = 2.5; | |
83 %! assert (q, qa); | |
87 %! q = prctile (1:4, pct, 1); | 84 %! q = prctile (1:4, pct, 1); |
88 %! qa = [1, 2, 3, 4]; | 85 %! qa = [1, 2, 3, 4]; |
89 %! assert (q, qa); | 86 %! assert (q, qa); |
90 %! q = prctile (1:4, pct, 2); | 87 %! q = prctile (1:4, pct, 2); |
91 %! qa = 2.5000; | 88 %! qa = 2.5; |
89 %! assert (q, qa); | |
90 | |
91 %!test | |
92 %! pct = [50 75]; | |
93 %! q = prctile (1:4, pct); | |
94 %! qa = [2.5 3.5]; | |
95 %! assert (q, qa); | |
96 %! q = prctile (1:4, pct, 1); | |
97 %! qa = [1, 2, 3, 4; 1, 2, 3, 4]; | |
98 %! assert (q, qa); | |
99 %! q = prctile (1:4, pct, 2); | |
100 %! qa = [2.5 3.5]; | |
92 %! assert (q, qa); | 101 %! assert (q, qa); |
93 | 102 |
94 %!test | 103 %!test |
95 %! pct = 50; | 104 %! pct = 50; |
96 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393 | 105 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393 |