Why do I get empty double vector
이전 댓글 표시
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.
답변 (1개)
James Tursa
2020년 11월 10일
편집: James Tursa
2020년 11월 10일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!