function y = entropy_1d (x)
% Return the entropy of the input vector x.
% If the input is an MxN array I, first convert to 1D vector, e.g., 
% x = reshape(I, 1, MxN)
% y = entropy_1d (x)
%
% Author: Dr. David Pan, Univ. of Alabama in Huntsville
x = double(x); % Make sure the range is not restrained by uint8
xmin = min(x);
xmax = max(x);
len = length(x);
range = xmax - xmin + 1;
cnt = zeros(1, range);

for i = 1: len
    index = x(i) - xmin + 1;
    cnt(index) = cnt(index) + 1;
end

y = 0;
for i = 1: range
    if cnt(i) ~= 0
        p = cnt(i)/len;
        y = y - p*log2(p); 
    end
end

        
