RosettaCodeData/Task/Entropy/MATLAB/entropy-1.m

8 lines
162 B
Mathematica
Raw Permalink Normal View History

2013-10-27 22:24:23 +00:00
function E = entropy(d)
if ischar(d), d=abs(d); end;
[Y,I,J] = unique(d);
H = sparse(J,1,1);
p = full(H(H>0))/length(d);
E = -sum(p.*log2(p));
end;