I was trying to find the entropy of a certain probability distribution in MATLAB. For p, I tried doing
E = -sum(p .* log2(p))
and Echeck = entropy(p)
Shouldn't E and Echeck be same?
The matlab help on entropy does say Entropy is defined as -sum(p.*log2(p)) where p contains the histogram counts returned from imhist.But also that entropy converts any class other than logical to uint8 for the histogram count calculation since it is actually trying to calculate the entropy of a grayscale image and hence wants the pixel values to be discrete. So I guess it's incorrect to use this function for my purpose? Is there a good alternative?
I used open entropy
to check the code, and there is a line:
if ~islogical(I)
I = im2uint8(I);
end
p = imhist(I(:));
which mean that the input is converted to uint8, and then the function computes the entropy of the histogram of the input, and not of the input itself.
That explains the difference.