How to set up an approximately equal conditional statement?

19 ビュー (過去 30 日間)
Nicholas Henderson
Nicholas Henderson 2016 年 9 月 26 日
移動済み: Stephen23 2025 年 7 月 11 日
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?

回答 (4 件)

Star Strider
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.
  2 件のコメント
Prudhvi Raj Sunkara
Prudhvi Raj Sunkara 2020 年 4 月 19 日
thanx
Star Strider
Star Strider 2020 年 4 月 19 日
My pleasure.

サインインしてコメントする。


John D'Errico
John D'Errico 2016 年 9 月 26 日
What would this tell you?
abs(x - y)

Walter Roberson
Walter Roberson 2016 年 9 月 26 日
You may wish to look at ismembertol()

Jennifer Rebbin
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)
ans = logical
1
isapprox(1,1.02,AbsoluteTolerance=0.01)
ans = logical
0

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by