# How to automize Shannon's Entropy calculation using a MM function

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];
SeedRandom[31];
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.

ClearAll[sE]

• In the Do loop format, I face this problem, leading to Indeterminate output. How do you handle zeros in the matrix used? – Tugrul Temel Jan 25 at 21:16
• @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]]] &; – kglr Jan 25 at 22:00