Skip to content

Commit

Permalink
refined entropy
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Feb 19, 2016
1 parent 2e0b3b8 commit 22a3ef5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions chapter01/entropy.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
function z = entropy(x)
% Compute entropy z=H(x) of a discrete variable x.
% Input:
% x: a vectors of integers
% x: a integer vectors
% Output:
% z: entropy z=H(x)
% Written by Mo Chen ([email protected]).
n = numel(x);
x = reshape(x,1,n);
[u,~,label] = unique(x);
p = full(mean(sparse(1:n,label,1,n,numel(u),n),1));
z = -dot(p,log2(p+eps));
z = max(0,z);
[u,~,x] = unique(x);
k = numel(u);
idx = 1:n;
Mx = sparse(idx,x,1,n,k,n);
Px = nonzeros(mean(Mx,1));
Hx = -dot(Px,log2(Px));
z = max(0,Hx);

0 comments on commit 22a3ef5

Please sign in to comment.