필터 지우기
필터 지우기

Sum(X) = 1 but Sum(X)==1 produces logical 0

조회 수: 1 (최근 30일)
George
George 2014년 7월 22일
댓글: George 2014년 7월 22일
Can someone please explain this?
sum([Node(temp).pbelief])
ans =
1.0000
K>> sum([Node(temp).pbelief])==1
ans =
0
By the way:
Node(temp(1)).pbelief
ans =
0.0024
K>> Node(temp(2)).pbelief
ans =
0.9976

채택된 답변

Namita Vishnubhotla
Namita Vishnubhotla 2014년 7월 22일
This does not seem like a limitation/bug in MATLAB. Use the long fixed decimal format before displaying your values:
>> format long
There is a possibility for a discrepancy in the values.
For example, sum([Node(temp).pbelief]) could actually hold 1.0000100, in which case the equality condition is bound to fail.
You could also use "format hex" to view the hexadecimal representation of your sum, and compare it to the hexadecimal representation of 1.
In general, it is not advisable to compare the equality of two values directly, as you have done in this case. It is better to compare their difference to a small predefined tolerance value.
>> abs(sum([Node(temp).pbelief]) – 1) <= 0.0001
  댓글 수: 1
George
George 2014년 7월 22일
Thanks. It was indeed a precision issue.
Solved by using your command

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

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2014년 7월 22일

James Tursa
James Tursa 2014년 7월 22일
Note that when a number is exactly an integer, and you are only printing one scalar value, MATLAB prints it without any trailing 0's. But if the number is not exactly an integer, MATLAB will print trailing 0's, indicating that there is a non-zero digit down the line somewhere even if it is not printed to the screen. E.g.,
>> format short
>> 1
ans =
1
>> 1.0000001
ans =
1.0000

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by