matthiasm@8: function H = entropy(v, scale) matthiasm@8: % ENTROPY Entropy log base 2 matthiasm@8: % H = entropy(v) matthiasm@8: % If v is a matrix, we compute the entropy of each column matthiasm@8: % matthiasm@8: % % H = entropy(v,1) means we scale the result so that it lies in [0,1] matthiasm@8: matthiasm@8: if nargin < 2, scale = 0; end matthiasm@8: matthiasm@8: v = v + (v==0); matthiasm@8: H = -1 * sum(v .* log2(v), 1); % sum the rows matthiasm@8: matthiasm@8: if scale matthiasm@8: n = size(v, 1); matthiasm@8: unif = normalise(ones(n,1)); matthiasm@8: H = H / entropy(unif); matthiasm@8: end