1 ## Copyright (C) 2003 Alberto Terruzzi <t-albert@libero.it>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{table} =} tabulate (@var{data}, @var{edges})
19 ## Compute a frequency table.
21 ## For vector data, the function counts the number of
22 ## values in data that fall between the elements in the edges vector
23 ## (which must contain monotonically non-decreasing values). @var{table} is a
25 ## The first column of @var{table} is the number of bin, the second
26 ## is the number of instances in each class (absolute frequency). The
27 ## third column contains the percentage of each value (relative
28 ## frequency) and the fourth column contains the cumulative frequency.
30 ## If @var{edges} is missed the width of each class is unitary, if @var{edges}
31 ## is a scalar then represent the number of classes, or you can define the
33 ## @var{table}(@var{k}, 2) will count the value @var{data} (@var{i}) if
34 ## @var{edges} (@var{k}) <= @var{data} (@var{i}) < @var{edges} (@var{k}+1).
35 ## The last bin will count the value of @var{data} (@var{i}) if
36 ## @var{edges}(@var{k}) <= @var{data} (@var{i}) <= @var{edges} (@var{k}+1).
37 ## Values outside the values in @var{edges} are not counted. Use -inf and inf
38 ## in @var{edges} to include all values.
39 ## Tabulate with no output arguments returns a formatted table in the
45 ## sphere_radius = [1:0.05:2.5];
46 ## tabulate (sphere_radius)
49 ## Tabulate returns 2 bins, the first contains the sphere with radius
50 ## between 1 and 2 mm excluded, and the second one contains the sphere with
51 ## radius between 2 and 3 mm.
54 ## tabulate (sphere_radius, 10)
57 ## Tabulate returns ten bins.
60 ## tabulate (sphere_radius, [1, 1.5, 2, 2.5])
63 ## Tabulate returns three bins, the first contains the sphere with radius
64 ## between 1 and 1.5 mm excluded, the second one contains the sphere with
65 ## radius between 1.5 and 2 mm excluded, and the third contains the sphere with
66 ## radius between 2 and 2.5 mm.
69 ## bar (table (:, 1), table (:, 2))
74 ## @seealso{bar, pareto}
77 ## Author: Alberto Terruzzi <t-albert@libero.it>
79 ## Created: 13 February 2003
81 function table = tabulate (varargin)
83 if nargin < 1 || nargin > 2
88 if isvector (data) != 1
89 error ("data must be a vector.");
95 if nargin == 1 edges = 1:1:max(data)+1;
96 else edges = varargin{2};
105 bins=length(edges)-1;
106 # initialize freqency table
107 freqtable = zeros(bins,4);
111 freqtable(k,2)=length(find (data >= edges(k) & data < edges(k+1)));
113 freqtable(k,2)=length(find (data >= edges(k) & data <= edges(k+1)));
115 if k == 1 freqtable (k,4) = freqtable(k,2);
116 else freqtable(k,4) = freqtable(k-1,4) + freqtable(k,2);
120 freqtable(:,1) = edges(1:end-1)(:);
121 freqtable(:,3) = 100*freqtable(:,2)/n;
124 disp(" bin Fa Fr% Fc");
125 printf("%8g %5d %6.2f%% %5d\n",freqtable');
126 else table = freqtable;