필터 지우기
필터 지우기

Facing this weird comparison error of vector

조회 수: 1 (최근 30일)
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020년 10월 26일
댓글: Mahesh Babu Dhanekula 2020년 10월 27일
I have some data that needs to be read from a file.
contents of file are as follows:
point1
0,0,2.2
point2
1,0,2.2
so i have used the fread command to read contents and used the Newline command to store each line as a member of cell(4x1 in this case).
I used the following command to convert the point data in form of string into vector
A = str2num(strtrim(Lines{2}))
D = str2num(strtrim(Lines{4}))
Outputs: (Double)
A = [0,0,2.2000]
D = [1,0,2.2000]
Here comes the error when i tried to compare the 3rd elements of A & D and to my surprise the following comparison expression false:
A(3) == D(3)
ans = 0 (logical)
Can anybody say what is wrong in this that causing this type of result ?
Please share your thoughts
  댓글 수: 7
Stephen23
Stephen23 2020년 10월 27일
편집: Stephen23 2020년 10월 27일
"I forgot to add a point about i am modifying one point to get the vertices of geometry"
Ah, secret operations that accumulate floating point error :)
"Here after i am facing this comparison error as you said it might be attributed to floating point error."
Yes.
Use this to look at the exact values, you will see that they are not the same:
Tip: do not test floating point numbers for exact equivalence. Always compare the absolute difference against a tolerance:
abs(X-Y)<tol
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020년 10월 27일
Thanks Stephen, I will modify my logic accordingly.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by