Main Content

allbetween

R2026b

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 a scalar, vector, matrix, multidimensional 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 a scalar, vector, matrix, multidimensional array, table, or timetable. lower must be an object with the class methods lt (<) or le (<=).

    Input Data TypeBound BehaviorHow to Specify lower
    ArraySame lower bound for all elements of AScalar
    ArrayDifferent lower bound for each column of ARow vector
    ArrayDifferent lower bound for each row of AColumn vector
    ArraySame bound for all elements of AArray of the same size as A
    Table or timetableSame lower bound for all elements of AScalar
    Table or timetableDifferent lower bound for each variable of AOne-row table or timetable with variables from A
    Table or timetableDifferent lower bound for each element of ATable or timetable with the same variable names and number of rows as A

    Upper bound of the range, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. upper must be an object with the class methods lt (<) or le (<=).

    Input Data TypeBound BehaviorHow to Specify upper
    ArraySame upper bound for all elements of AScalar
    ArrayDifferent upper bound for each column of ARow vector
    ArrayDifferent upper bound for each row of AColumn vector
    ArraySame upper for all elements of AArray of the same size as A
    Table or timetableSame upper bound for all elements of AScalar
    Table or timetableDifferent upper bound for each variable of AOne-row table or timetable with variables from A
    Table or timetableDifferent upper bound for each element of ATable or timetable with the same variable names and number of rows as A

    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.

    Output Arguments

    collapse all

    Indicator that all values are within range, returned as a logical scalar. tf is logical 1 (true) if all elements in A are within the range defined by the lower and upper bounds. Otherwise, tf is logical 0 (false).

    Data Types: logical

    Extended Capabilities

    expand all

    Version History

    Introduced in R2025a

    expand all

    See Also

    | | | | | | | |