Search code examples
matlabentropyglcm

Calculating entropy from co-occurence matrix in Matlab


I am trying to extract the entropy from co-occurence matrices with zero entries in Matlab. From the definition of entropy of a co-occurence matrix:

definition of entropy
has to be calculated, where cij stands for the (i,j) entry of the co-occurence matrix. Thus it seems to me that if there is a single zero entry, the entropy will be undefined. Do you set some sort of lower limit to log(x) when x = 0, or how do you deal with it?

Link to a pdf with the definition of entropy for the GLCM: http://www.code.ucsd.edu/pcosman/glcm.pdf

EDIT: Thanks for the suggestions on how to deal with log(0), but the equation actually calls for evaluating 0*log(0) which is 0 anyway. It would have been easier to explain if I could use formulas, but maybe my question was more mathematical anyway, and thus on the wrong forum.


Solution

  • I generally use the following workaround to avoid this issue:

    X = C .* log2(C + (C == 0));
    entropy = -sum(X(:));
    

    For those entries of C (the co-occurrence matrix) that are 0, the argument of the logarithm function is 1 since the expression (C == 0) is evaluated as 1.