MATLAB: Different answer using inbuilt function and own code for entropy

entropyimage processingImage Processing Toolbox

When I use the inbuilt function and my own code for entropy, the answer is different.
E = -sum(Rarray(Rarray>0) .* log2(Rarray(Rarray>0)));
E2 = entropy(Rarray);
Entropy with Writen code = -768.0522
Entropy wth Function = 1.7366

Best Answer

Simply edit entropy.m and you'll see exactly how they do it:
% calculate histogram counts
p = imhist(I(:));
% remove zero entries in p
p(p==0) = [];
% normalize p so that sum(p) is one.
p = p ./ numel(I);
E = -sum(p.*log2(p));