function [counts_,vals_] = xtab1(data)

%XTAB1 -- Build (and print) a uni-dimensional frequency table from data.
%
% XTAB1(DATA)
% COUNTS = XTAB1(DATA)
% [COUNTS,VALS] = XTAB1(DATA)
%
% This is a specialized variant of the function XTAB for the important
% special case of uni-dimensional cross-tabulation.
% DATA must be either a column vector or a matrix of K variables (cols).
% When there are K>1 variables, the rows in DATA are treated as composite
% values (see UNIQUE(...,'rows')).
% The interpretation of the output arguments is the same as in XTAB except
% that VALS is a plain column-vector (or a matrix with K columns) instead
% of a cell array containing a single cell containing this vector/matrix.
%
% See also XTAB, XTAB2, PRINT_XTAB, TABULATE, HIST, UNIQUE.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.1 $  $Date: 2002/01/17 19:09 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

[N,K] = size(data) ;

if (K==1)   % single variable
   [vals,foo,keys] = unique(data) ;
else        % multiple variables --> composite values
   [vals,foo,keys] = unique(data,'rows') ;
end

if (N>0)
   counts = hist(keys,size(vals,1))' ;
else  % degenerate case -- empty input
   counts = 0 ; vals = [] ;
end

%-- Print a frequency table when no outputs are requested
if (nargout==0)
  print_xtab(counts,{vals}) ;   
else  % export values and don't print anything
  counts_ = counts ;
  vals_ = vals ;
end

%% Return COUNTS_ and VALS_
%%%%% End of XTAB1.M
