home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / base / table.m < prev    next >
Text File  |  1997-02-19  |  2KB  |  54 lines

  1. ## Copyright (C) 1995, 1996, 1997  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  [t, l_x] = table (x)
  18. ##         [t, l_x, l_y] = table (x, y)
  19. ##
  20. ## Create a contingency table t from data vectors.  The l vectors are
  21. ## the corresponding levels.
  22. ##
  23. ## Currently, only 1- and 2-dimensional tables are supported.
  24.   
  25. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  26. ## Description:  Cross tabulation
  27.   
  28. function [t, v, w] = table (x, y)
  29.   
  30.   if (nargin == 1)
  31.     if !(is_vector (x))
  32.       error ("table:  x must be a vector");
  33.     endif
  34.     v = values (x);
  35.     for i = 1 : length (v)
  36.       t(i) = sum (x == v(i) | isnan (v(i)) * isnan (x));
  37.     endfor
  38.   elseif (nargin == 2)
  39.     if !(is_vector (x) && is_vector (y) && (length (x) == length (y)))
  40.       error ("table:  x and y must be vectors of the same length");
  41.     endif
  42.     v = values (x);
  43.     w = values (y);
  44.     for i = 1 : length (v)
  45.       for j = 1 : length (w)
  46.     t(i,j) = sum ((x == v(i) | isnan (v(i)) * isnan (x)) &
  47.               (y == w(j) | isnan (w(j)) * isnan (y)));
  48.       endfor
  49.     endfor
  50.   else
  51.     usage ("[t, l_x, ...] = table (x, ...)");
  52.   endif
  53.   
  54. endfunction