function H = entropy(x)

% ENTROPY   Entropy of a distribution.
%
%  H = ENTROPY(X)
%
%  The elements of X should be non-negative numbers indicating
%  frequencies or probabilities. When X is a vector of probabilities, 
%  ENTROPY(X) is the corresponding entropy  E = - sum(x .* log2(x)).
%  For matrices, ENTROPY(X) is a row vector with the entropies over 
%  each column.
%
%  Examples:  entropy([1/4 1/4 1/4 1/4]') == 2     % probabilities
%             entropy([50 50 50 50]') == 2         % frequencies
%
%  See also INFOGAIN, XTAB, BINOMIAL_LOGL.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2000/3/6 22:00 $
%
% 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


[rows,cols] = size(x) ;
if ((rows == 1) & (cols > 1))
  % flip row-vector into a column-vector
  x = x' ;
  rows = cols ;
end

x = x ./ repmat(sum(x),rows,1) ;    % frequencies --> probabilities

%-- Calculate base-2 logarithms of all positive entries in X
%   (Note that  lim(x*log(x))=0  when x-->0.)
log_x = zeros(size(x)) ;
idx = find(x > 0) ;
log_x(idx) = log2(x(idx)) ;

H = - sum(x .* log_x) ;

%%  Return H
%%%%% End of ENTROPY.M
