matlab - How can I vectorize the entropy calculation? -
i trying entropy every column, matrix looks this:
0.5 0.3333 0.2 0 0.3333 0.4 0.5 0.3333 0.4
every column add one, however, there's zeros in matrix, if log2(arr(i,:)), there -inf in result whole thing won't work
in practice have huge matrix, want program run fast, there work around?
here's solution, works fast p .* log2(p)?
log2p = log2(p); log2p(log2p==-inf)=0; entropy = entropy - p .* log2p;
in matlab 0^0
equal 1
. , since log2(1)==0
, can use , rewrite entropy function as
p.*log2(p) = log2(p.^p)
then example get
>> log2(p.^p) ans = -0.5000 -0.5283 -0.4644 0 -0.5283 -0.5288 -0.5000 -0.5283 -0.5288
Comments
Post a Comment