Facing this weird comparison error of vector

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 Kommentare

Mathieu NOE
Mathieu NOE am 26 Okt. 2020
hi
I remember some info about str2num :
help str2num CAUTION: str2num uses EVAL to convert the input argument, so side
effects can occur if S contains calls to functions. Use
STR2DOUBLE to avoid such side effects or when S contains a single
number. Also spaces can be significant. For instance, str2num('1+2i') and
str2num('1 + 2i') produce x = 1+2i while str2num('1 +2i') produces
x = [1 2i]. These problems are also avoided when you use STR2DOUBLE.:
so please try with STR2DOUBLE
Stephen23
Stephen23 am 26 Okt. 2020
"Can anybody say what is wrong in this that causing this type of result ?"
Most likely some floating point error, i.e. those two values are not exactly the same.
Thanks for letting me know, but why it is occuring in first place, even those were the same values that are read from file.
Can you let me know about any work around methods to overcome this problem ?
str2double simly returns nothing when i used on the it.
str2double(Lines{2})
ans = NaN
Stephen23
Stephen23 am 27 Okt. 2020
@Mahesh Babu Dhanekula: please upload the data file by clicking the paperclip button, and show us the exact code that you use to import and compare the data.
fid = fopen('data.txt','r');
Lines = strsplit(fread(fid,'*char').',newline);
fclose(fid);
A = str2num(strtrim(Lines{2}));
D = str2num(strtrim(Lines{5}));
I forgot to add a point about i am modifying one point to get the vertices of geometry. There will be one point among each set of vertices that is duplicated and I have to modify it to get correct coordinates of particular vertex.
In the uploaded file D needs to be modified, So i have done that by doing as follows:
D = B + C - A
Here after i am facing this comparison error as you said it might be attributed to floating point error.
Stephen23
Stephen23 am 27 Okt. 2020
Bearbeitet: Stephen23 am 27 Okt. 2020
"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
Thanks Stephen, I will modify my logic accordingly.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Produkte

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by