I have this small code. If statement fails to execute when x(i)<=10^0 say for 10^-5, 10^-1 or 10^0 even though matrix x contains the given elements
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
K Chiranjeevi Reddy
am 10 Mär. 2015
Kommentiert: K Chiranjeevi Reddy
am 10 Mär. 2015
clear all
clc
y=10^-8;
z=1;
x(1)=y;
for i=0:16
for j=1:9
x(z+1)=x(z)+y;
z=z+1;
end
y=y*10;
end
[~,m]=size(x);
u=1;
for i=1:m
if x(i)==0.01
u=u+1
end
end
1 Kommentar
Akzeptierte Antwort
Michael Haderlein
am 10 Mär. 2015
For the third time today, the answer is floating point arithmetics:
>> [f,ind]=min(abs(x-.01))
f =
1.7347e-18
ind =
55
So the 55th element of x is close to 0.01, but it actually isn't 0.01. Comparing double values, you should give some threshold under which two numbers are considered equal. E.q.
if abs(x(i)-.01)<1e-6
@Guillaume has given a good link for further information here: http://de.mathworks.com/matlabcentral/answers/182376-find-function-and-vectors-gives-empty-matrix-1-by-0#comment_270986
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!