~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to macros/statistics/tabul.sci

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2005-01-09 22:58:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050109225821-473xr8vhgugxxx5j
Tags: 3.0-12
changed configure.in to build scilab's own malloc.o, closes: #255869

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function [m] = tabul(X, order)
 
2
   //
 
3
   //  PURPOSE
 
4
   //     This function computes the frequency of values of 
 
5
   //     the components of a vector or matrix X of numbers or
 
6
   //     string characters.
 
7
   //
 
8
   //      If X  is a numerical  vector or matrix then
 
9
   //      m  is a  two column matrix who contains in 
 
10
   //      the first column the distinct values of X 
 
11
   //      and  in the other column the number of occurrences 
 
12
   //      of those values (m(i,2) is the number of occurrences
 
13
   //      of m(i,1)).
 
14
   //
 
15
   //      If X is a vector or matrix of strings, m is  a list 
 
16
   //      whose  first member is a string vector composed with 
 
17
   //      the distinct values of X and the second member is a 
 
18
   //      vector whose components are the number of occurrences
 
19
   //      of those values ( m(i)(2) is the number of occurrences
 
20
   //      of the string m(i)(1) ).
 
21
   //
 
22
   //      The optional parameter order must be "d" or "i" 
 
23
   //      (by default order = "d") and it gives the order
 
24
   //      of the distinct vector values of X (first column
 
25
   //      or first part of m) :
 
26
   //         order = "d" means that these values are sorted
 
27
   //                     in decreasing order
 
28
   //               = "i" means by increasing order 
 
29
   //
 
30
   //  AUTHORS
 
31
   //      Original version by Carlos Klimann
 
32
   //      This version by Jean-Sebastien Giet & Bruno Pincon
 
33
   //
 
34
   //  date: 1999-04-09 (original version)
 
35
   //        2003-Mars-26 (new version)
 
36
   //
 
37
   //  NOTES
 
38
   //      The new version :
 
39
   //          is faster (by using no more loop)
 
40
   //          performs a complete check of the input arguments
 
41
   //          add the order option
 
42
   //          used gsort in place of sort : the sort function
 
43
   //          sorts strings vector not the usual way !
 
44
 
 
45
   rhs = argn(2)
 
46
   if rhs<1 | 2<rhs then
 
47
      error(" tabul : 1 or 2 input argument(s) needed")
 
48
   elseif rhs == 1 then
 
49
      order = "d"
 
50
   end
 
51
   typeX = type(X)
 
52
   if typeX ~= 1 & typeX ~= 10 then
 
53
      error(" tabul : 1st input argument must be a vector/matrix of numbers or strings")
 
54
   end
 
55
   if type(order) ~= 10 then
 
56
      error(" tabul : 2d input argument (order) must be ""i"" or ""d""")
 
57
   end
 
58
   if order~="i" &  order~="d" then
 
59
      error(" tabul : 2d input argument (order) must be ""i"" or ""d""")
 
60
   end
 
61
   if ( X = [] ) then
 
62
      m = %nan
 
63
      return
 
64
   end
 
65
   
 
66
   X = X(:)
 
67
   X = gsort(X,"g",order)
 
68
   n = size(X,"*")
 
69
   ind = [find(X(1:$-1)~=X(2:$)) n]
 
70
 
 
71
   val = X(ind)
 
72
   occ = diff([0 ind])'
 
73
   
 
74
   if typeX == 1 then
 
75
      m = [val occ]
 
76
   else // X (and so val) is a vector of strings
 
77
      m = list(val,occ)
 
78
   end
 
79
   
 
80
endfunction