Why does following code don't give any output?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Chetan Dahal
el 13 de Abr. de 2022
Respondida: Rik
el 13 de Abr. de 2022
for i =0.1:0.1:12
if(i==3)
fprintf('Hello\n')
end
end
0 comentarios
Respuesta aceptada
Rik
el 13 de Abr. de 2022
Because the number 3 doesn't exist in your array:
data=0.1:0.1:12;
[~,ind]=min(abs(data-3));
data(ind)
Now, that looks like 3, but let's investigate if it is exactly 3:
data(ind)-3
Close, but not exact. Since you used ==, Matlab will only give you the fprintf if the match is exact.
The solution is to use a tolerance.
for i =0.1:0.1:12
if abs(i-3)<(10*eps)
fprintf('Hello\n')
end
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!