isbetween
Determine which elements are within specified range
Syntax
Description
TF = isbetween(
determines which elements in the input data are within the interval defined by the
lower and upper bounds and returns a logical array the same size as the input data.
By default, the interval is a closed interval. A
,lower
,upper
)TF
contains
logical 1
(true
) where the corresponding
element is within the specified range, and logical 0
(false
) otherwise.
TF = isbetween(
specifies the type of interval. For example,
A
,lower
,upper
,intervalType
)isbetween(A,lower,upper,"open")
finds elements in
A
that are within the open interval
(lower,upper)
.
TF = isbetween(___,
specifies options using one or more name-value arguments in addition to any of the
input argument combinations in the previous syntaxes. For example, for an input
table Name=Value
)A
, TF =
isbetween(A,lower,upper,OutputFormat="tabular")
returns the output
TF
as a table.
Examples
Create a row vector, and find elements in the vector that are within a specified range.
A = [1 3 5 7 9]; TF = isbetween(A,2,7)
TF = 1×5 logical array
0 1 1 1 0
Display the values of the elements that are within the range.
val = A(TF)
val = 1×3
3 5 7
Create a numeric matrix.
A = repmat(1:7,4,1)
A = 4×7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
Determine which elements in each row are within a specified range. The lower bound, specified as a column vector, sets the lower bound of the range for the corresponding row of the input data. The upper bound, specified as a scalar, sets the upper bound for all rows of the input data.
lower = [1; 2; 3; 4]; upper = 6; TF = isbetween(A,lower,upper)
TF = 4×7 logical array
1 1 1 1 1 1 0
0 1 1 1 1 1 0
0 0 1 1 1 1 0
0 0 0 1 1 1 0
Create a row vector, and determine which elements in the vector are within a specified range. Specify the interval type as "open"
to exclude the lower and upper bounds.
A = 1:7;
TF = isbetween(A,3,6,"open")
TF = 1×7 logical array
0 0 0 1 1 0 0
Include the lower bound by specifying the interval type as "closedleft"
. Any elements equal to the lower bound return 1 (true
).
TF2 = isbetween(A,3,6,"closedleft")
TF2 = 1×7 logical array
0 0 1 1 1 0 0
Include the upper bound by specifying the interval type as "closedright"
. Any elements equal to the upper bound return 1 (true
).
TF3 = isbetween(A,3,6,"closedright")
TF3 = 1×7 logical array
0 0 0 1 1 1 0
Compare the interval types by displaying the array elements within the specified ranges.
val = A(TF)
val = 1×2
4 5
val2 = A(TF2)
val2 = 1×3
3 4 5
val3 = A(TF3)
val3 = 1×3
4 5 6
Create an array of datetime
values.
A = datetime(2024,5,16:2:26)
A = 1×6 datetime
16-May-2024 18-May-2024 20-May-2024 22-May-2024 24-May-2024 26-May-2024
Specify the lower and upper bounds for a range of dates.
lower = datetime(2024,01,01);
upper = "2024-05-22";
Determine which elements are within the closed interval.
TF = isbetween(A,lower,upper)
TF = 1×6 logical array
1 1 1 1 0 0
Display the values of elements that are within the range.
val = A(TF)
val = 1×4 datetime
16-May-2024 18-May-2024 20-May-2024 22-May-2024
Create a table of data.
T = table([1;3;5;7],[2;4;6;8])
T=4×2 table
Var1 Var2
____ ____
1 2
3 4
5 6
7 8
Return a table that indicates the elements in T
are within a specified range.
TF = isbetween(T,3,7,OutputFormat="tabular")
TF=4×2 table
Var1 Var2
_____ _____
false false
true true
true true
true false
Replace any elements in the input table that are outside of the specified range with a missing value.
T.Var1(~TF.Var1) = missing; T.Var2(~TF.Var2) = missing
T=4×2 table
Var1 Var2
____ ____
NaN NaN
3 4
5 6
7 NaN
Create a table with three variables of different data types.
num = rand(6,1); num2 = single(rand(6,1)); dt = datetime(2016:2021,1,1)'; T = table(num,num2,dt)
T=6×3 table
num num2 dt
_______ _______ ___________
0.81472 0.2785 01-Jan-2016
0.90579 0.54688 01-Jan-2017
0.12699 0.95751 01-Jan-2018
0.91338 0.96489 01-Jan-2019
0.63236 0.15761 01-Jan-2020
0.09754 0.97059 01-Jan-2021
Specify the bounds for variables of different data types in one-row tables.
lower = table(0.2,single(0.1),datetime(2018,1,1),VariableNames=["num" "num2" "dt"])
lower=1×3 table
num num2 dt
___ ____ ___________
0.2 0.1 01-Jan-2018
upper = table(0.9,Inf,datetime(2020,1,1),VariableNames=["num" "num2" "dt"])
upper=1×3 table
num num2 dt
___ ____ ___________
0.9 Inf 01-Jan-2020
Determine which elements in the num
and dt
variables are within the specified ranges.
TF = isbetween(T,lower,upper,DataVariables=["num" "dt"])
TF = 6×3 logical array
1 0 0
0 0 0
0 0 1
0 0 1
1 0 1
0 0 0
Alternatively, if you specify the bounds as one-row tables containing only the bounds for num
and dt
, you do not need to specify the DataVariables
name-value argument.
Input Arguments
Input data, specified as an array, table, or timetable.
A
must be an object with the class methods
lt
(<) or le
(<=).
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| categorical
| datetime
| duration
| table
| timetable
Lower bound of the range, specified as an array or one-row table. The
lower and upper bounds must either be the same size or have sizes that are
compatible.
lower
must be an object with the class methods
lt
(<) or le
(<=).
To use the same lower bound for all elements of
A
, specifylower
as a scalar.To use different lower bounds for each column or row in
A
, specifylower
as a row or column vector, respectively.To use a different lower bound for each data element, specify
lower
as an array of the same size asA
.
For tabular input data, when the table variables to operate on have different data types, specify the lower bound as a one-row table. The variable names of the one-row table must be the same as the names of the table variables to operate on.
Upper bound of the range, specified as an array or one-row table. The
lower and upper bounds must either be the same size or have sizes that are
compatible.
upper
must be an object with the class methods
lt
(<) or le
(<=).
To use the same upper bound for all elements of
A
, specifyupper
as a scalar.To use different upper bounds for each column or row in
A
, specifyupper
as a row or column vector, respectively.To use a different upper bound for each data element, specify
upper
as an array of the same size asA
.
For tabular input data, when the table variables to operate on have different data types, specify the upper bound as a one-row table. The variable names of the one-row table must be the same as the names of the table variables to operate on.
Type of interval that defines the range of allowed values, specified as one of the values in this table.
Type of Interval | Diagram | Description |
---|---|---|
|
| Include |
|
| Exclude |
|
| Exclude
|
|
| Include
|
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 =
isbetween(A,lower,upper,OutputFormat="tabular")
Table or timetable variables to operate on, specified as one of the
values in this table. TF
contains logical
0
(false
) for variables not
specified by DataVariables
unless the value of
OutputFormat
is
"tabular"
.
If you do not specify DataVariables
,
isbetween
operates on all variables in
A
.
Indexing Scheme | Values to Specify | Examples |
---|---|---|
Variable name |
|
|
Variable index |
|
|
Function handle |
|
|
Variable type |
|
|
Example: TF = isbetween(T,lower,upper,DataVariables=["Var1"
"Var2" "Var4"])
Output data type, specified as one of these values:
"logical"
— Return the outputTF
as a logical array."tabular"
— For table input data, return the outputTF
as a table. For timetable input data, return the outputTF
as a timetable.
Example: TF =
isbetween(T,lower,upper,OutputFormat="tabular")
Extended Capabilities
The
isbetween
function supports tall arrays with the following usage
notes and limitations:
First argument must be a tall datetime, tall duration, or tall string array.
Name-value arguments
DataVariables
andOutputFormat
are not supported.
For more information, see Tall Arrays.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Usage notes and limitations:
Input argument
A
must be a distributed datetime, distributed duration, or distributed string array.Name-value arguments
DataVariables
andOutputFormat
are not supported.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2014bDetermine which elements are within a range that you specify for an input array of
any data type, a table, or a timetable. Previously, isbetween
supported only date and time input data.
For data in a table or timetable, you can return a table or timetable containing
logical values instead of a logical array by using the
OutputFormat
name-value argument. You can also specify
tabular variables to operate on by using the DataVariables
name-value argument.
isbetween
supports implicit expansion when the arguments are
datetime
or duration
arrays. Previously,
implicit expansion was supported for only numeric and string data types.
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: United States.
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)