Main Content

isbetween

Determine which elements are within specified range

Description

TF = isbetween(A,lower,upper) 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 1 (true) where the corresponding element is within the specified range, and 0 (false) otherwise.

example

TF = isbetween(A,lower,upper,intervalType) determines which elements in the input data are within the type of interval specified by intervalType. For example, isbetween(A,lower,upper,"open") finds elements in A that are within the open interval (lower,upper).

example

TF = isbetween(___,Name=Value) 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 output TF 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 = 1x5 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 = 4x7 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 = 1x7 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 = 1x7 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 = 1x7 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 = 1x6 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 = 1x6 logical array

   1   1   1   1   0   0

Display the values of elements that are within the range.

val = A(TF)
val = 1x4 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 = 6x3 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 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, specify lower as a scalar.

  • To use different lower bounds for each column or row in A, specify lower 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 as A.

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, specify upper as a scalar.

  • To use different upper bounds for each column or row in A, specify upper 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 as A.

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 and upper.

"open"

Open interval (lower, upper)

Exclude lower and upper.

"openleft" or "closedright"

Half-open interval (lower, upper]

Exclude lower and include upper.

"openleft" and "closedright" have the same behavior.

"openright" or "closedleft"

Half-open interval [lower, upper)

Include lower and exclude upper.

"openright" and "closedleft" have the same behavior.

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 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 SchemeValues to SpecifyExamples

Variable names

  • A string scalar or character vector

  • A string array or cell array of character vectors

  • A pattern object

  • "A" or 'A' — A variable named A

  • ["A" "B"] or {'A','B'} — Two variables named A and B

  • "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 table

  • A vector of numbers

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 (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:

  • "logical" — Return the output TF as a logical array.

  • "tabular" — For table input data, return the output TF as a table. For timetable input data, return the output TF as a timetable.

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

Extended Capabilities

Version History

Introduced in R2014b

expand all

See Also

| | | | | | |