Main Content

allbetween

Determine if all elements are within specified range

Since R2025a

    Description

    tf = allbetween(A,lower,upper) returns logical 1 (true) if all elements in A are within the range defined by the lower and upper bounds. Otherwise, allbetween returns logical 0 (false). By default, the range is a closed interval.

    example

    tf = allbetween(A,lower,upper,intervalType) specifies the type of interval. For example, allbetween(A,lower,upper,"open") determines if elements in A are within the open interval (lower,upper).

    example

    tf = allbetween(___,DataVariables=vars) specifies the table or timetable variables to operate on in addition to any of the input argument combinations in previous syntaxes. For example, for table A, allbetween(A,lower,upper,DataVariables="Var1") determines if elements in table variable Var1 are within the specified range.

    example

    Examples

    collapse all

    Create a row vector, and determine if all elements in the vector are within [2, 7].

    A = [1 3 5 7 9];
    tf = allbetween(A,2,7)
    tf = logical
       0
    
    

    To determine which elements are outside the specified range, use the isbetween function.

    Create a row vector, and determine if all elements in the vector are within (1, 10). Specify the interval type as "open" to exclude the lower and upper bounds.

    A = 1:7;
    tf = allbetween(A,1,10,"open")
    tf = logical
       0
    
    

    The result is logical 0 (false) because the first element in the vector is equal to the lower bound. Include the lower bound in the allowed range by specifying the interval type as "closedleft".

    tf2 = allbetween(A,1,10,"closedleft")
    tf2 = logical
       1
    
    

    Create a table with two variables of different data types.

    num = rand(6,1);
    dt = datetime(2016:2021,1,1)';
    T = table(num,dt)
    T=6×2 table
          num          dt     
        _______    ___________
    
        0.81472    01-Jan-2016
        0.90579    01-Jan-2017
        0.12699    01-Jan-2018
        0.91338    01-Jan-2019
        0.63236    01-Jan-2020
        0.09754    01-Jan-2021
    
    

    Specify the bounds for variables of different data types in one-row tables.

    lower = table(0,datetime(2018,1,1),VariableNames=["num" "dt"])
    lower=1×2 table
        num        dt     
        ___    ___________
    
         0     01-Jan-2018
    
    
    upper = table(Inf,datetime(2020,1,1),VariableNames=["num" "dt"])
    upper=1×2 table
        num        dt     
        ___    ___________
    
        Inf    01-Jan-2020
    
    

    Determine if all elements in the num variable are within the specified range.

    tf = allbetween(T,lower,upper,DataVariables="num")
    tf = logical
       1
    
    

    Determine if all elements in both variables are within the specified ranges.

    tf2 = allbetween(T,lower,upper)
    tf2 = logical
       0
    
    

    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"

    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.

    Table or timetable variables to operate on, specified as one of the values in this table.

    allbetween operates only on elements in the specified variables in A.

    Indexing SchemeValues to SpecifyExamples

    Variable name

    • 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 = allbetween(T,lower,upper,DataVariables=["Var1" "Var2" "Var4"]) determines if the elements in the Var1, Var2, and Var4 variables in table T are within the specified range.

    Version History

    Introduced in R2025a

    See Also

    | | | | | | | |