isbetween - Determine which elements are within specified range - MATLAB (original) (raw)

Determine which elements are within specified range

Syntax

Description

TF = isbetween([A](#bugc7uu-1-t),[lower](#bugc7uu-1-tlower),[upper](#mw%5Ff2647faa-8566-452c-a731-693131a3dda5)) 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. TF contains logical 1 (true) where the corresponding element is within the specified range, and logical 0 (false) otherwise.

example

TF = isbetween([A](#bugc7uu-1-t),[lower](#bugc7uu-1-tlower),[upper](#mw%5Ff2647faa-8566-452c-a731-693131a3dda5),[intervalType](#mw%5F7c043e92-203b-4e3f-910f-9b33db1b971d)) specifies the type of interval. For example,isbetween(A,lower,upper,"open") finds elements inA that are within the open interval(lower,upper).

example

TF = isbetween(___,[Name=Value](#namevaluepairarguments)) 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 A, TF = isbetween(A,lower,upper,OutputFormat="tabular") returns the outputTF as a table.

example

Examples

collapse all

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.

Create a numeric matrix.

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.

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 = 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

collapse all

Input data, specified as an array, table, or timetable.

A must be an object with the class methodslt (<) 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 arecompatible.lower must be an object with the class methodslt (<) or le (<=).

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 arecompatible.upper must be an object with the class methodslt (<) or le (<=).

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
"closed" (default) Closed interval [lower, upper] Include lower andupper.
"open" Open interval (lower, upper) Exclude lower andupper.
"openleft" or"closedright" ![Half-open interval (lower, upper]](https://www.mathworks.com/help/matlab/ref/interval-notation-04.png) Exclude lower and includeupper."openleft" and "closedright" have the same behavior.
"openright" or"closedleft" ![Half-open interval lower, upper) Include lower and excludeupper."openright" and "closedleft" have the same behavior.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=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 logical0 (false) for variables not specified by DataVariables unless the value ofOutputFormat is"tabular".

If you do not specify DataVariables,isbetween operates on all variables inA.

Indexing Scheme Values to Specify Examples
Variable name A string scalar or character vectorA string array or cell array of character vectorsA pattern object "A" or 'A' — A variable named A["A" "B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index An index number that refers to the location of a variable in the tableA vector of numbersA logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 (false) values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Function handle A function handle that takes a table variable as input and returns a logical scalar @isnumeric — All the variables containing numeric values
Variable type A vartype subscript that selects variables of a specified type vartype("numeric") — All the variables containing numeric values

Example: TF = isbetween(T,lower,upper,DataVariables=["Var1" "Var2" "Var4"])

Output data type, specified as one of these values:

Example: TF = isbetween(T,lower,upper,OutputFormat="tabular")

Extended Capabilities

expand all

Theisbetween function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced in R2014b

expand all

Determine 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 theOutputFormat 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 aredatetime or duration arrays. Previously, implicit expansion was supported for only numeric and string data types.