Symantic Error in built-in find function

3 views (last 30 days)
Hi everyone. I am using MATLAB built-in function "find" for finding the number of instances of a particular value present in every array or vector. But the problem is that this "find" misses instances. For example: I have a vector of 100 values ranging from 2 to 8. I use find function to calculate the number instances of every value. The problem here is that "find" function detects only lets say 80 or 90 instances. It leaves out others. Any suggestions please?

Accepted Answer

Walter Roberson
Walter Roberson on 22 May 2015
Floating point round off. Those locations only look like they are the value you want to find, but they have a very slightly different value, perhaps 1E-16 smaller. If you subtract the value you are trying to match from those locations you will find that you get a non-zero value.
In particular never code a find that is searching for a floating point constant that is not an integer. Decimal numbers whose fraction ends in any digit other than 5 is almost certainly an error (and most of the fractions ending in 5 are usually wrong.)
find(x==0.3)
is only going to match entries by chance.
Do not test for floating point equality unless you really understand how numbers are represented. Instead check to see if the location is close enough to the desired result, such as
find(abs(x-0.3)<1E-10)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!