Code to Find entropy for the given set of probability using MATLAB

Code:


n=input('How many messages are there? : ');
p=[ ];
for x= 1:n
    sprintf('Enter the value of the message %d ''s probability',x)
    p(x)=input('p = ');
end
entr=0;
if sum(p)==1
    for x=1:n
        entr=entr+p(x)*log(1/p(x));
    end
else

    disp('Total probability should be equal to 1, Please enter the correct value....')
end
entr=entr/log(2);
sprintf('entropy = %f bits/message',entr)
   
   
Output:



How many messages are there? : 4

Enter the value of the message 1 's probability

p = .3

Enter the value of the message 2 's probability

p = .1

Enter the value of the message 3 's probability

p = .2

ans =

Enter the value of the message 4 's probability

p = .4

ans =

entropy = 1.846439 bits/message

 


0 comments:

Post a Comment