How to set up an approximately equal conditional statement?
19 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(4 个)
Star Strider
2016-9-26
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.
Jennifer Rebbin
2025-7-10
移动:Stephen23
2025-7-11
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!