Why do I get empty double vector
Show older comments
I'm a new user of Matlab and I have met a weird problem. I tried to create a simple function like this:
y=0:0.0025:2.5;
x=2*y;
disp(x(y==1.15));
and I will get a 1x0 empty double row vector instead of 2.3. When I tried y==1.12 or y==1.16, I could get the correct result. I suppose that might because of the double integer but I don't really understand.
Answers (1)
James Tursa
on 10 Nov 2020
Edited: James Tursa
on 10 Nov 2020
0 votes
This is normal floating point arithmetic behavior. None of 0.0025 or 1.15 or 1.12 or 1.16 can be represented in IEEE double precision exactly, so you will get small differences showing up when compared to exact decimal representations in calculations, and these differences can propagate through further calculations. If this makes a difference to your algorithm, you will need to rewrite your algorithm to account for these effects. E.g., using tolerances when making comparisons.
Categories
Find more on Logical 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!