How to set up an approximately equal conditional statement?

13 Ansichten (letzte 30 Tage)
Nicholas Henderson
Nicholas Henderson am 26 Sep. 2016
Verschoben: Stephen23 am 11 Jul. 2025
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?

Antworten (4)

Star Strider
Star Strider am 26 Sep. 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.

John D'Errico
John D'Errico am 26 Sep. 2016
What would this tell you?
abs(x - y)

Walter Roberson
Walter Roberson am 26 Sep. 2016
You may wish to look at ismembertol()

Jennifer Rebbin
Jennifer Rebbin am 10 Jul. 2025
Verschoben: Stephen23 am 11 Jul. 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)
ans = logical
1
isapprox(1,1.02,AbsoluteTolerance=0.01)
ans = logical
0

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by