Skipping 0 value in matrix computation wich lead to NaN.

조회 수: 1 (최근 30일)
Derry Suia
Derry Suia 2016년 7월 21일
댓글: Derry Suia 2016년 7월 21일
Hello, I'm Derry.
first, forgive me for my bad english,
I'm currently working on my Thesis, but I've got something that bothering me. I tried to calculate an image Contrast Weighted Entropy (CWH) value, using this script :
img = imread('image.tif');
img = imresize(img, [32, 32]);
[M N] = size(img);
numPix = M*N;
[count, bins] = imhist(img);
p = count/numPix;
u=mean(count);
val = (p-u).*p.* (log(p)/0.3010299957)*numPix;
val2=sum(val);
CWH=-1*val2;
imshow (img);
but it give me a NaN value because it contain a 0 value on "image.tif" matrix.
the point is, is there any way to pass a value of 0 so that the calculation can be completed?
thank you in advance
Derry Suia
  댓글 수: 2
Stephen23
Stephen23 2016년 7월 21일
Note that you shoudl avoid using pi as a variable name, because this is a very important inbuilt constant pi. For the same reason you should never use the names size, cell, length, i, j, etc, etc.
Derry Suia
Derry Suia 2016년 7월 21일
Hello Mr. Cobeldick,
thank you for your response, I did change "pi" as value name, but it still contain 0 value in matrix.
do you have any suggestion for my problem?
thanks
Derry Suia

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

채택된 답변

Thorsten
Thorsten 2016년 7월 21일
편집: Thorsten 2016년 7월 21일
Before you take the log, get rid of the 0's:
p = p(p ~= 0);
  댓글 수: 1
Derry Suia
Derry Suia 2016년 7월 21일
wow, it works like a charm!
thank you for for your answer, kind Sir/Madam!
cheers!
Derry Suia

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by