Equals numbers returns not equal

조회 수: 4 (최근 30일)
SAFA YILDIRIM
SAFA YILDIRIM 2020년 5월 5일
댓글: SAFA YILDIRIM 2020년 5월 5일
Hello I have a code like below:
figure
arr = [0.0207,0.6371,0.7504,0.1585,0.3091,0.5112];
for i=1:6
item = arr(i);
image = NewImage;
for r=1:row
for c=1:column
temp = image(r,c,1);
if item ~= temp
image(r,c,:) = 0;
end
end
end
subplot(3,2,i),imshow(image);
end
temp variable and item are both equal to 0.0207 but program flow enters if block. What is the problem?

채택된 답변

Clemens Gersch
Clemens Gersch 2020년 5월 5일
편집: Clemens Gersch 2020년 5월 5일
Are you sure that they are exactly the same?
Try
format long
and then display your values again. I guess that temp is only roughly 0.0207 and not exactly.
If that is the reason for your problem, you can try the following:
if item ~= round(temp,4)
  댓글 수: 1
SAFA YILDIRIM
SAFA YILDIRIM 2020년 5월 5일
Thank you so much. It solved the problem :)

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by