Hello.
The following command returns 1:
ismember(-0.6, -1:.1:.9)
but this next command returns 0:
ismember(-0.1, -1:.1:.9)
even though -0.1 clearly is in -1:.1:.9.
Does anybody know what's going on?
Hello.
The following command returns 1:
ismember(-0.6, -1:.1:.9)
but this next command returns 0:
ismember(-0.1, -1:.1:.9)
even though -0.1 clearly is in -1:.1:.9.
Does anybody know what's going on?
Floating point error.
The number 0.1 is REALLY REALLY CLOSE to the value in the vector, but not exactly. Some numbers can not be represented exactly in a binary computer.
What are you trying to do exactly, there are many thresholding techniques that can work here if we know your situation better.
The problem is that when you start at -1.0 and add 0.1 repeatedly you get a slightly different number than if you specify -0.1 directly. This is because of floating-point error accumulation. Just like 1/3 cannot be represented exactly in decimal (it becomes 0.33333...), many decimal numbers cannot be represented exactly in binary. 0.1 when converted to binary is actually a very close approximation to 0.1. Because there is a slight error when you do arithmetic with this floating point number this small difference accumulates and becomes bigger and bigger.
From http://www.mathworks.com/matlabcentral/newsreader/view_thread/246492:
Ashwini Deshpande wrote:
I have a matrix as follows,
a = 0:0.1:1;
when I tried to find whether 0.300 is present in the matrix
a
, using the following procedure,>> [tf, loc]=ismember(0.3,a)
I got the following result:
tf = 0 loc = 0
But it's suppose to give me,
tf = 1
andloc =4
.
You're getting the correct result, actually. Look what happens:
v=0:.1:1; n=0.3; sprintf('%30.20f\n',v(4),n) %{ 0.30000000000000004000 % <- result from vec 0.29999999999999999000 % <- result from handwritten number %} format hex; [v(4),n,v(4)-n].' %{ 3fd3333333333334 % <- vec 3fd3333333333333 % <- num 3c90000000000000 % <- diff %} format;
Also, peruse http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F
What you are seeing is a consequence of floating point representations for numbers, in that the number 0.1 is not exactly representable as a floating point value. The colon operator used to generate the vector does so through successive additions of the value 0.1 and accumulates very small errors in the process. The value in the vector that you think is -0.1 is actually slightly different from -0.1 due to these errors, and is also different from the value of -0.1 that results when you declare it directly. Take this example:
>> format long % Changes display to show more decimal places
>> vec = -1:0.1:0.9;
>> vec(10)+0.1
ans =
2.775557561562891e-017
Notice that you don't get 0 as an answer, since the -0.1 in the vector is ever so slightly different in absolute value from the 0.1 you are adding to it. However, if you were to create your vector without using the colon operator, you would get the answer you desire:
>> vec = [-1 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0 ...
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9];
>> ismember(-0.1,vec)
ans =
1
In short, explicitly declaring a value of -0.1 gives an ever-so-slightly different number than generating the value of -0.1 through successively adding 0.1 to -1.
Thanks so much everyone!! I tried to upvote you all but because this is an unregistered account it isn't letting me..