isapprox
Description
determines which corresponding elements in the two input arrays are approximately equal and
returns a logical array. TF
= isapprox(A
,B
)TF
contains 1
(true
) where the corresponding elements are within the region of approximate
equality, and 0
(false
) otherwise. The
default relative and absolute tolerances are both 1e-15
, or if
A
or B
is of type single
,
5e-7
.
specifies options using one or more name-value arguments. For example,
TF
= isapprox(A
,B
,Name=Value
)isapprox(A,B,RelativeTolerance=1e-10)
determines approximate equality
using the specified relative tolerance and an absolute tolerance of
0
.
Examples
Compare Two Values
Create two numeric scalar values and compare them for equality. The ==
operator returns logical 0
(false
) because the values differ by a very small amount and are not exactly equal.
A = sin(3/4*pi); B = 1/sqrt(2); A == B
ans = logical
0
To account for round-off error, compare the values for approximate equality. The isapprox
function returns logical 1
(true
) because the difference between the values of A
and B
is within the default tolerance.
TF = isapprox(A,B)
TF = logical
1
A - B
ans = 1.1102e-16
Accept More Noise
Compare the value of π in double precision, which has 15 digits after the decimal point, and elements in a numeric vector for approximate equality.
format long
A = pi
A = 3.141592653589793
B = [2*asin(1) integral(@(x) 1./sqrt(1-x.^2),-1,1) round(pi,10) 22/7]
B = 1×4
3.141592653589793 3.141592653589721 3.141592653600000 3.142857142857143
TF = isapprox(A,B)
TF = 1x4 logical array
1 0 0 0
The isapprox
function returns logical 1
(true
) for only the first element. The difference between all other elements in B
and the value of A
is greater than the default tolerance. To accept more noise in the input data, specify the relative and absolute tolerance level as "tight"
.
TF2 = isapprox(A,B,"tight")
TF2 = 1x4 logical array
1 1 0 0
The isapprox
function returns logical 1
(true
) for only the first and second elements. To accept even more noise in the input data, specify the relative and absolute tolerance level as "loose"
.
TF3 = isapprox(A,B,"loose")
TF3 = 1x4 logical array
1 1 1 0
The isapprox
function returns logical 1
(true
) for the first three elements. To accept even more noise in the input data, specify the relative and absolute tolerance level as "veryloose"
.
TF4 = isapprox(A,B,"veryloose")
TF4 = 1x4 logical array
1 1 1 1
Specify Absolute Tolerance
Compare two sets of measurements from two different instruments for approximate equality. Specify the absolute tolerance as 0.02
, which is the known precision of the instruments. If you specify just the absolute tolerance, the relative tolerance is 0
.
A = [1.00 2.00 3.10 NaN]; B = [0.99 2.01 3.15 NaN]; TF = isapprox(A,B,AbsoluteTolerance=0.02)
TF = 1x4 logical array
1 1 0 0
Input Arguments
A
, B
— Input data
arrays
Input data, specified as arrays. A
and B
must
either be the same size or have sizes that are compatible. If the input arrays are complex, then they must be of type
single
or double
.
Specifying isapprox(A,B)
is the same as specifying
isapprox(B,A)
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Complex Number Support: Yes
tol
— Relative and absolute tolerance level
"verytight"
(default) | "tight"
| "loose"
| "veryloose"
Relative and absolute tolerance level, specified as one of the values in this table. For more information, see Region of Approximate Equality for Tolerances.
If you specify tol
, you cannot specify the
RelativeTolerance
or AbsoluteTolerance
name-value arguments.
Tolerance Level | Tolerance When Both | Tolerance When Either |
---|---|---|
| 1e-15 | 5e-7 |
| 1e-12 | 1e-6 |
| 1e-8 | 1e-4 |
| 1e-3 | 1e-2 |
Data Types: string
| char
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: TF = isapprox(A,B,RelativeTolerance=1e-10)
RelativeTolerance
— Relative tolerance
scalar in range [0, 1)
Relative tolerance, specified as a scalar in the range [0, 1)
.
The relative tolerance defines the maximum allowed difference between approximately equal elements as a fraction of the element with greater magnitude.
If you do not specify the
RelativeTolerance
orAbsoluteTolerance
name-value arguments, the relative tolerance is1e-15
, or ifA
orB
are of typesingle
,5e-7
.If you specify the
RelativeTolerance
name-value argument and do not specify theAbsoluteTolerance
name-value argument, the absolute tolerance is0
.
Example: isapprox(A,B,RelativeTolerance=1e-10)
uses a relative
tolerance of 1e-10
and an absolute tolerance of
0
.
Example: isapprox(A,B,RelativeTolerance=1e-10,AbsoluteTolerance=1e-8)
uses a relative tolerance of 1e-10
and an absolute tolerance of
1e-8
.
Data Types: double
| single
AbsoluteTolerance
— Absolute tolerance
nonnegative scalar
Absolute tolerance, specified as a nonnegative scalar.
The absolute tolerance defines the maximum allowed difference between approximately equal elements as a fixed number.
If you do not specify the
RelativeTolerance
orAbsoluteTolerance
name-value arguments, the absolute tolerance is1e-15
, or ifA
orB
are of typesingle
,5e-7
.If you specify the
AbsoluteTolerance
name-value argument and do not specify theRelativeTolerance
name-value argument, the relative tolerance is0
.
Example: isapprox(A,B,AbsoluteTolerance=1e-8)
uses an absolute
tolerance of 1e-8
and a relative tolerance of
0
.
Example: isapprox(A,B,RelativeTolerance=1e-10,AbsoluteTolerance=1e-8)
uses a relative tolerance of 1e-10
and an absolute tolerance of
1e-8
.
Data Types: double
| single
Algorithms
Region of Approximate Equality
Tolerance | Algorithm | Region of Approximate Equality |
---|---|---|
Relative tolerance | , where A and B are
corresponding elements in the input arrays and relTol is the
relative tolerance. |
|
Absolute tolerance | , where A and B are
corresponding elements in the input arrays and absTol is the
absolute tolerance. |
|
Relative and absolute tolerances | , where A and B are
corresponding elements in the input arrays, relTol is the
relative tolerance, and absTol is the absolute
tolerance. |
|
Version History
Introduced in R2024b
See Also
eq
| isequal
| ismembertol
| verifyEqual
| eps
| uniquetol
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)