1
## Copyright (C) 2008 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{y} =} prctile (@var{x}, @var{p})
21
## @deftypefnx {Function File} {@var{q} =} prctile (@var{x}, @var{p}, @var{dim})
22
## For a sample @var{x}, compute the the quantiles, @var{y}, corresponding
23
## to the cumulative probability values, P, in percent. All non-numeric
24
## values (NaNs) of X are ignored.
26
## If @var{x} is a matrix, compute the the percentiles for each column and
27
## return them in a matrix, such that the i-th row of @var{y} contains the
28
## @var{p}(i)th percentiles of each column of @var{x}.
30
## The optional argument @var{dim} determines the dimension along which
31
## the percentiles are calculated. If @var{dim} is omitted, and @var{x} is
32
## a vector or matrix, it defaults to 1 (column wise quantiles). In the
33
## instance that @var{x} is a N-d array, @var{dim} defaults to the first
34
## dimension whose size greater than unity.
38
## Author: Ben Abbott <bpabbott@mac.com>
39
## Description: Matlab style prctile function.
41
function q = prctile (x, p, dim)
43
if (nargin < 1 || nargin > 3)
48
p = 100*[0.00 0.25, 0.50, 0.75, 1.00];
53
## If a matrix or vector, use the 1st dimension.
56
## If an N-d array, use the firt dimension with a length > 1.
57
dim = find (size(v) != 1);
64
## Convert from percent.
67
## The 5th method is compatible with Matlab.
70
## Call the quantile function
71
q = quantile (x, p, dim, method);
77
%! q = prctile (1:4, pct, 1);
80
%! q = prctile (1:4, pct, 2);
86
%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
87
%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
88
%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
89
%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
90
%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
92
%! q = prctile (x, pct, 1);
93
%! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
94
%! assert (q, qa, tol);
95
%! q = prctile (x, pct, 2);
96
%! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9312];
97
%! assert (q, qa, tol);
102
%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
103
%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
104
%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
105
%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
106
%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
108
%! q = prctile (x, pct, 1);
109
%! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
110
%! assert (q, qa, tol);
112
%! q = prctile (x, pct, 1);
113
%! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.1585];
114
%! assert (q, qa, tol);
116
%! q = prctile (x, pct, 1);
117
%! qa = [0.4288, 0.7978, 0.3296, 0.5567, 0.1585];
118
%! assert (q, qa, tol);
123
%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
124
%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
125
%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
126
%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
127
%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
129
%! q = prctile (x, pct, 1);
130
%! qa = [0.2795, 0.7978, 0.6477, 0.5567, 0.7307];
131
%! assert (q, qa, tol);
132
%! q = prctile (x, pct, 2);
133
%! qa = [0.1148; 0.2041; 0.7307; 0.6477; 0.9312];
134
%! assert (q, qa, tol);
139
%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
140
%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
141
%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
142
%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
143
%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
145
%! q = prctile (x, pct, 2);
146
%! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9322];
147
%! assert (q, qa, tol);
149
%! q = prctile (x, pct, 2);
150
%! qa = [0.1270; 0.2041; 0.5567; 0.6477; 0.9322];
151
%! assert (q, qa, tol);
153
%! q = prctile (x, pct, 2);
154
%! qa = [0.1270; 0.2041; 0.6437; 0.6477; 0.9322];
155
%! assert (q, qa, tol);