# Shanon entropy of a matrix

10 views (last 30 days)
ali eskandari on 18 Mar 2021
Answered: Jan on 18 Mar 2021
I defined a matrix like A and I want to calculate its entropy and when I use the entropy function it returns zero :
A = [2,3,4;4,5,6;10,12,1]
entropy(A)
ans =
0
but when I read the matlab entropy help, it mentioned that this funtion will return the entropy of a grayscale image, so I have decided to apply mat2gray to convert my matrix into the grayscale, and now it returns 2.9477.
b= mat2gray(A)
b =
0.0909 0.1818 0.2727
0.2727 0.3636 0.4545
0.8182 1.0000 0
entropy(b)
ans =
2.9477

Jan on 18 Mar 2021
As the documentation tells, the input is expected to be a gray scale image. Then value over 1.0 are limit to 1.0 and you matrix is interpreted as [1, 1, 1; 1, 1, 1; 1, 1, 1] with zero entropy.
Converting the matrix by mat2gray divides the values by the larges element after subtracting the smalles element:
A = [2,3,4; 4,5,6; 10,12,1]
mA = min(A(:));
B = (A - mA) / (max(A(:)) - mA) % same as MAT2GRAY
As far as I understand, this is the correct way to determine the entropy.