How to set up an approximately equal conditional statement?
Mostrar comentarios más antiguos
with a tolerance of a certain value like .05, how would I check if the values for example x = 1 and y = 1.02 were approximately equal?
Respuestas (4)
Star Strider
el 26 de Sept. de 2016
I would do something like this:
x = 1;
y = 1.02;
tol = 0.05;
app_eq = @(x,y,tol) abs(x-y)<tol;
Out = app_eq(x,y,tol)
Out =
1
The result ‘Out’ is a logical value of 1 or true.
2 comentarios
Prudhvi Raj Sunkara
el 19 de Abr. de 2020
thanx
Star Strider
el 19 de Abr. de 2020
My pleasure.
John D'Errico
el 26 de Sept. de 2016
What would this tell you?
abs(x - y)
Walter Roberson
el 26 de Sept. de 2016
0 votos
You may wish to look at ismembertol()
Jennifer Rebbin
el 10 de Jul. de 2025
Movida: Stephen23
el 11 de Jul. de 2025
Starting in R2024b, you can use the isapprox function to determine if two input arrays are approximately equal. Specify the maximum allowed difference between elements using the AbsoluteTolerance name-value argument.
isapprox(1,1.02,AbsoluteTolerance=0.05)
isapprox(1,1.02,AbsoluteTolerance=0.01)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!