Why does MATLAB not finding a element from my matrix?
2 views (last 30 days)
Show older comments
x = 0.4:0.01:0.5
idx = find(x==0.41)
2 Comments
Dyuman Joshi
on 27 Mar 2024
Welcome to the world of floating point numbers! Here not all numbers can be represented exactly in binary form.
Stephen has provided an extensive list of material which you can refer to - for detailed information and explainations regarding the same.
Accepted Answer
Stephen23
on 27 Mar 2024
Moved: Dyuman Joshi
on 27 Mar 2024
Testing for exact equivalence of binary floating point numbers should be avoided.
Compare the values, are they actually the same? (hint: no):
x = 0.4:0.01:0.5;
fprintf('%.40f\n', x(2), 0.41)
The recommended approach is to compare the absolute difference against a tolerance:
tol = 1e-5;
abs(x(2)-0.41)<tol
More information on this topic:
This is worth reading as well:
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!