Please have a look at the following example:
A = 0:0.1:0.4;find(A == 0.3)ans = Empty matrix: 1-by-0find(A == 0.1+0.1+0.1)ans = 4
This is in my opinion expected behavior, as 0.1 can't be represented accurately with floating point numbers. What's bugging me is the following:
A = 5.8:0.1:6A = 5.8000 5.9000 6.0000find(A == 5.9)ans = 2%%Found it!
A = 5.8:0.1:6.1A = 5.8000 5.9000 6.0000 6.1000find(A == 5.9)ans = Empty matrix: 1-by-0%%Didn't find it!
find(A == 5.8+0.1)ans = 2%%Found it again!
For the record, linspace results in the same results.
A = linspace(5.8, 6.0, 3)A = 5.8000 5.9000 6.0000find(A == 5.9)ans = 2A = linspace(5.8, 6.1, 4)A = 5.8000 5.9000 6.0000 6.1000find(A == 5.9)ans = Empty matrix: 1-by-0find(A == 5.8+0.1)ans = 2
Notice that the only difference is that the vectors are going to 6.1 instead of 6.0. So, what's going on? Are the following two actually the same: x = [a:b:c] and y = linspace(a,c,(c-a)/b+1)?
A = 5.8:0.1:6.1A = 5.8000 5.9000 6.0000 6.1000B = linspace(5.8,6.1,4)B = 5.8000 5.9000 6.0000 6.1000A == Bans = 1 1 1 1
It might appear that way… But the answer is of course no, they're not the same!
x = -0.1:0.1:0.3x = -0.1000 0 0.1000 0.2000 0.3000y = linspace(-0.1,0.3,5)y = -0.1000 0 0.1000 0.2000 0.3000x == yans = 1 1 0 0 1
So, what happens when you do A = 5.8:0.1:6? How are the numbers created? And how can the following be explained?
A = 5.8:0.1:6;B = 5.8:0.1:6.1;A(2)-B(2)ans = 8.8818e-016eps(5.9)ans = 8.8818e-016
Best Answer