]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/tabulate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / tabulate.m
1 ## Copyright (C) 2003 Alberto Terruzzi <t-albert@libero.it>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{table} =} tabulate (@var{data}, @var{edges})
18 ##
19 ## Compute a frequency table.
20 ##
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
24 ## matrix.
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.
29 ##
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
32 ## width of each bin.
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
40 ## command window. 
41 ##
42 ## Example
43 ##
44 ## @example
45 ## sphere_radius = [1:0.05:2.5];
46 ## tabulate (sphere_radius)
47 ## @end example
48 ##
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.
52 ##
53 ## @example
54 ## tabulate (sphere_radius, 10)
55 ## @end example
56 ##
57 ## Tabulate returns ten bins.
58 ##
59 ## @example
60 ## tabulate (sphere_radius, [1, 1.5, 2, 2.5])
61 ## @end example
62 ##
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. 
67 ##
68 ## @example
69 ## bar (table (:, 1), table (:, 2))
70 ## @end example
71 ##
72 ## draw histogram.
73 ##
74 ## @seealso{bar, pareto}
75 ## @end deftypefn
76
77 ## Author: Alberto Terruzzi <t-albert@libero.it>
78 ## Version: 1.0
79 ## Created: 13 February 2003
80
81 function table = tabulate (varargin)
82
83   if nargin < 1 || nargin > 2
84     print_usage;
85   endif
86
87   data = varargin{1};
88   if isvector (data) != 1
89     error ("data must be a vector.");
90   endif
91   n = length(data);
92   m = min(data);
93   M = max(data);
94
95   if nargin == 1 edges = 1:1:max(data)+1;
96   else edges = varargin{2};
97   end 
98
99   if isscalar(edges)
100     h=(M-m)/edges;
101     edges = [m:h:M];
102   end
103
104   # number of classes
105   bins=length(edges)-1;
106   # initialize freqency table
107   freqtable = zeros(bins,4);
108
109   for k=1:1:bins;
110     if k != bins
111       freqtable(k,2)=length(find (data >= edges(k) & data < edges(k+1)));
112     else
113       freqtable(k,2)=length(find (data >= edges(k) & data <= edges(k+1)));
114     end
115     if k == 1 freqtable (k,4) = freqtable(k,2);
116     else freqtable(k,4) = freqtable(k-1,4) + freqtable(k,2); 
117     end
118   end
119
120   freqtable(:,1) = edges(1:end-1)(:);
121   freqtable(:,3) = 100*freqtable(:,2)/n;
122
123   if nargout == 0
124     disp("     bin     Fa       Fr%        Fc");
125     printf("%8g  %5d    %6.2f%%    %5d\n",freqtable');
126   else table = freqtable;
127   end
128
129 endfunction