I am trying to plot for binary entropy

조회 수: 19 (최근 30일)
Sam17
Sam17 2018년 1월 31일
댓글: yair mazal 2021년 3월 17일
I am trying to plot binary entropy and the value of entropy is coming as NaN but, it shouldn't be NaN, it should be 0.
Here's my code:
%Question No 1
%H(p) versus probability of binary logic 1 (p)
p= 0:0.5:1
h= p.*(abs(log2(p)))+ (1-p).*(abs(log(1-p)))
plot(p,h);
  댓글 수: 1
yair mazal
yair mazal 2021년 3월 17일
Obviously you also miss the "2" in the second call to the log function. You're using log in the base of 10.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 31일
If your equations are correct then nan is the correct answer for p values of 0 and p values of 1.
Suppose p = 0. Then log2(p) is -infinity. p.*log2(p) is 0 * -infinity which is nan.
For p=1 something similar takes place.

카테고리

Help CenterFile Exchange에서 Log Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by