How to set up an approximately equal conditional statement?
조회 수: 12 (최근 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.
댓글 수: 2
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!