1 ## Copyright (C) 2008-2012 Ben Abbott
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{q} =} prctile (@var{x})
21 ## @deftypefnx {Function File} {@var{q} =} prctile (@var{x}, @var{p})
22 ## @deftypefnx {Function File} {@var{q} =} prctile (@var{x}, @var{p}, @var{dim})
23 ## For a sample @var{x}, compute the quantiles, @var{q}, corresponding
24 ## to the cumulative probability values, @var{p}, in percent. All non-numeric
25 ## values (NaNs) of @var{x} are ignored.
27 ## If @var{x} is a matrix, compute the percentiles for each column and
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}.
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
33 ## the percentiles are calculated. If @var{dim} is omitted, and @var{x} is
34 ## a vector or matrix, it defaults to 1 (column-wise quantiles). When
35 ## @var{x} is an N-D array, @var{dim} defaults to the first non-singleton
40 ## Author: Ben Abbott <bpabbott@mac.com>
41 ## Description: Matlab style prctile function.
43 function q = prctile (x, p = [], dim)
45 if (nargin < 1 || nargin > 3)
49 if (! (isnumeric (x) || islogical (x)))
50 error ("prctile: X must be a numeric vector or matrix");
54 p = [0, 25, 50, 75, 100];
57 if (! (isnumeric (p) && isvector (p)))
58 error ("prctile: P must be a numeric vector");
64 ## If a matrix or vector, always use 1st dimension.
67 ## If an N-d array, find the first non-singleton dimension.
68 (dim = find (sz > 1, 1)) || (dim = 1);
71 if (!(isscalar (dim) && dim == fix (dim))
72 || !(1 <= dim && dim <= nd))
73 error ("prctile: DIM must be an integer and a valid dimension");
77 ## Convert from percent to decimal.
80 q = quantile (x, p, dim);
87 %! q = prctile (1:4, pct, 1);
90 %! q = prctile (1:4, pct, 2);
96 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
97 %! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
98 %! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
99 %! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
100 %! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
102 %! q = prctile (x, pct, 1);
103 %! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
104 %! assert (q, qa, tol);
105 %! q = prctile (x, pct, 2);
106 %! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9312];
107 %! assert (q, qa, tol);
112 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
113 %! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
114 %! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
115 %! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
116 %! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
118 %! q = prctile (x, pct, 1);
119 %! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
120 %! assert (q, qa, tol);
122 %! q = prctile (x, pct, 1);
123 %! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.1585];
124 %! assert (q, qa, tol);
126 %! q = prctile (x, pct, 1);
127 %! qa = [0.4288, 0.7978, 0.3296, 0.5567, 0.1585];
128 %! assert (q, qa, tol);
133 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
134 %! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
135 %! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
136 %! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
137 %! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
139 %! q = prctile (x, pct, 1);
140 %! qa = [0.2795, 0.7978, 0.6477, 0.5567, 0.7307];
141 %! assert (q, qa, tol);
142 %! q = prctile (x, pct, 2);
143 %! qa = [0.1148; 0.2041; 0.7307; 0.6477; 0.9312];
144 %! assert (q, qa, tol);
149 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
150 %! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
151 %! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
152 %! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
153 %! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
155 %! q = prctile (x, pct, 2);
156 %! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9322];
157 %! assert (q, qa, tol);
159 %! q = prctile (x, pct, 2);
160 %! qa = [0.1270; 0.2041; 0.5567; 0.6477; 0.9322];
161 %! assert (q, qa, tol);
163 %! q = prctile (x, pct, 2);
164 %! qa = [0.1270; 0.2041; 0.6437; 0.6477; 0.9322];
165 %! assert (q, qa, tol);
167 %% Test input validation
169 %!error prctile (1, 2, 3, 4)
170 %!error prctile (['A'; 'B'], 10)
171 %!error prctile (1:10, [true, false])
172 %!error prctile (1:10, ones (2,2))
173 %!error prctile (1, 1, 1.5)
174 %!error prctile (1, 1, 0)
175 %!error prctile (1, 1, 3)