Hı, ı have a code P(i,j) is float and it has value 0-1 range , normally if we calculate log function must be equal positive value with initial minus. But this return to NaN what is the wrong
function entropy=ent(glcm)
P=glcm/sum(glcm(:));
entropy=0;
for i=1:8
for j=1:8
entropy=entropy+ P(i,j)*(-log(P(i,j));
end
end
end

댓글 수: 3

per isakson
per isakson 2020년 7월 21일
편집: per isakson 2020년 7월 21일
I fail to reproduce the error you report (on R2018)
>> ent(rand(8))
ans =
3.9484
>> ent(rand(8))
ans =
3.9371
>> ent(rand(8))
ans =
4.0212
What input to the function do you use?
Furkan b
Furkan b 2020년 7월 21일
편집: Furkan b 2020년 7월 21일
I want to calculate texture features like entropy using GLCM matrix this function part of my all code but it does not work. it return NaN.
Roger J
Roger J 2020년 7월 21일
What is the input to the function?
If glcm matrix is all zeros, then the sum is zero, and that creates NaN in P, and finally the returned entropy is NaN.
Also, don't name the variable "entropy", there is a common function named entropy(..).

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

답변 (1개)

Steven Lord
Steven Lord 2020년 7월 22일

0 개 추천

As Roger Jestes stated, if any element of P is exactly 0, you compute 0*(-log(0)) which is 0*inf which results in a NaN. This is the second of the indeterminant forms listed in this section of the Wikipedia page for NaN.
>> 0*(-log(0))
ans =
NaN
NaN plus anything is still NaN so once you add NaN to your entropy value once that's what it will remain.

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

태그

질문:

2020년 7월 21일

댓글:

2020년 7월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by