With the following code, I calculate Shannon's Entropy function HH1 for a given matrix mat. This code works just fine, but I need to replicate the same calculations for a large number of matrices. It will be convenient if the following calculations are combined in a MMA function called ShannonEntropy[...]:=, which accepts raw matrices as inputs, such matrices as mat1, mat2, etc.

ClearAll[n, mat, bbMM1, HH1];
n = 15;
mat = RandomReal[{0.001, 0.5}, {n, n}];
Do[bbMM1[i]=Transpose[Transpose[mat]/Total[mat]][[All, i]], {i,n}]    
HH1 = - Sum[Table[bbMM1[i][[j]]*Log[bbMM1[i][[j]]], {j, n}], {i, n}]  

Obviously, Do loops and Sum do not look nice. I would like to have a function of the following form:

ShannonEntropy[#]&/@{mat1, mat2, mat3}

where {mat1,mat2,mat3} is a vector of matrices for which entropy are to be calculated.

sE = Module[{m0 = Normalize[#, Total] & /@ Transpose[#]}, Total[-Log[m0] m0]] &;

sE[mat] == HH1


  • $\begingroup$ In the Do loop format, I face this problem, leading to Indeterminate output. How do you handle zeros in the matrix used? $\endgroup$ – Tugrul Temel Jan 25 at 21:16
  • 1
    $\begingroup$ @Tugrul, if the input matrix can have 0 entries, you can try sE = Quiet@ Block[{Indeterminate = 0}, Module[{m0 = Normalize[#, Total[#, 2] &] &@Transpose[#]}, Total[-Log[m0] m0]]] &; $\endgroup$ – kglr Jan 25 at 22:00
  • 1
    $\begingroup$ Thank you for the code. It smoothly workd for many matrices at one go. Thanks. $\endgroup$ – Tugrul Temel Jan 25 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.