Documentation

issortedrows

Determine if matrix or table rows are sorted

Description

example

TF = issortedrows(A)returns the logical scalar 1 (true) when the elements of the first column of a matrixAare listed in ascending order. Otherwise,issortedrowsreturns 0 (false). When the first column has consecutive repeated elements,issortedrowsdetermines whether the next column is in ascending order, and repeats this behavior for succeeding equal values.

example

TF = issortedrows(A,column)returns 1 whenAis sorted based on the columns specified in the vectorcolumn. For example,issortedrows(A,[4 6])first checks if the fourth column ofAis in ascending order, then checks if the sixth column is in ascending order to break ties.

example

TF = issortedrows(___,direction)returns 1 when the first column ofAis in the order specified bydirectionfor any of the previous syntaxes. For example,issortedrows(A,'monotonic')checks if the first column ofAis in ascending or descending order.directioncan also be a cell array of character vectors representing multiple directions for each column being checked. For example,issortedrows(A,[2 3],{'ascend' 'descend'})checks if the second column ofAis in ascending order, then checks if the third column is in descending order to break ties.

example

TF = issortedrows(___,Name,Value)specifies additional parameters for checking sort order. For example,issortedrows(A,'ComparisonMethod','abs')checks if the elements in the first column ofAare sorted by magnitude.

example

TF = issortedrows(tblA)checks if the rows of a table are in ascending order based on the elements in the first variable. If elements in the first variable are repeated, thenissortedrowschecks the elements in the second variable, and so on.

IftblAis a timetable, thenissortedrowschecks if the rows oftblAare in ascending order based on its row times. Row times of a timetable label the rows along the first dimension of the timetable.

example

TF = issortedrows(tblA,'RowNames')checks if the rows of a table are in ascending order based on its row names. Row names of a table label the rows along the first dimension of the table.

This syntax is not supported whentblAis a timetable.

example

TF = issortedrows(tblA,rowDimName)checks if the rows of a table are sorted by row labelsrowDimNamealong the first dimension.

  • IftblAis a table, then the labels are row names.

  • IftblAis a timetable, then the labels are row times.

example

TF = issortedrows(tblA,vars)checks if the rows of a table are in ascending order based on the elements in variablesvars. For example, ifAgeandWeightare variables oftblA, thenissortedrows(tblA,{'Age','Weight'})checks if the rows are in ascending order by age, then by weight to break ties.

  • IftblAis a table with row names, thenvarscan include the row names.

  • IftblAis a timetable, thenvarscan include the row times.

example

TF = issortedrows(___,direction)checks if a table is sorted in the order specified bydirectionfor any of the previous table syntaxes.directioncan be a single sort order such as'descend'or'monotonic', which is applied to each specified variable, row name, or row time.directioncan also be a cell array whose elements contain different sort orders for each specified variable, row name, or row time thatissortedrowsoperates on.

example

TF = issortedrows(___,Name,Value)specifies additional parameters for sorting tables. For example,issortedrows(tblA,'Var1','MissingPlacement','first')checks that missing elements inVar1, such asNaNorNaT, are placed at the beginning of the table.

Examples

collapse all

Create a matrix and determine if its rows are in ascending order based on the values in the first column. Since the first column has a repeated element,sortrowslooks to the second column to determine whether the matrix rows are sorted.

A = [1 2 9; 1 5 8; 4 0 7]
A =3×31 2 9 1 5 8 4 0 7
TF = issortedrows(A)
TF =logical1

Determine if the rows ofAare in ascending order based on the values in the third column.

TF = issortedrows(A,3)
TF =logical0

Determine if the rows ofAare in descending order based on the values in the third column.

TF = issortedrows(A,3,'descend')
TF =logical1

Create a matrix containing complex numbers, and determine if its rows are in ascending order based on the real parts of the elements in the first column. Since the elements in the first column have equal real parts,issortedrowsthen checks the imaginary parts to break the tie.

A = [1+i 2i; 1+2i 3+4i]
A =2×2 complex1.0000 + 1.0000i 0.0000 + 2.0000i 1.0000 + 2.0000i 3.0000 + 4.0000i
TF = issortedrows(A,'ComparisonMethod','real')
TF =logical1

For a table that describes patient information for five people, determine how the rows of the table are sorted.

Create a table with four variables, and determine if the rows of the table are in ascending order based on age. Since the age variable contains a repeated element,issortedrowsthen checks the next column (Height) to break the tie.

LastName = {'Sweet';'Jacobson';'Wang';'Joiner';'Berger'}; Age = [38;38;40;43;49]; Height = [69;71;64;67;64]; Weight = [176;163;131;133;119]; BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80]; tblA = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)
tblA=5×4 tableAge Height Weight BloodPressure ___ ______ ______ _____________ Sweet 38 69 176 124 93 Jacobson 38 71 163 109 77 Wang 40 64 131 125 83 Joiner 43 67 133 117 75 Berger 49 64 119 122 80
TF = issortedrows(tblA)
TF =logical1

Check if the table rows are sorted by last name, which are the row names fortblA.

TF = issortedrows(tblA,'RowNames')
TF =logical0

Check if the table rows are in ascending order by age, then in descending order by weight.

TF = issortedrows(tblA,{'Age','Weight'},{'ascend','descend'})
TF =logical1

Create a timetable, and check that the rows of the timetable are in ascending order based on the row times. Also check that missing elements are placed last.

Time = [seconds(1:3) NaN NaN]'; TT = timetable(Time,[98;97.5;97.9;98.1;99.9],[120;111;119;117;112],...'VariableNames',{'Temperature','Distance'})
TT=5×3 timetableTime Temperature Distance _______ ___________ ________ 1 sec 98 120 2 sec 97.5 111 3 sec 97.9 119 NaN sec 98.1 117 NaN sec 99.9 112
TF = issortedrows(TT,'Time','MissingPlacement','last')
TF =logical1

Input Arguments

collapse all

Input array, specified as a column vector or matrix.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string|categorical|datetime|duration
Complex Number Support:Yes

Column sorting vector, specified as a nonzero integer scalar or a vector of nonzero integers. Each specified integer value indicates a column to check for sort order. Negative integers indicate that the sort order is descending.

Sorting direction, specified as one of the following:

  • 'ascend'(default) — Checks if data is in ascending order. Data can contain consecutive repeated elements.

  • 'descend'— Checks if data is in descending order. Data can contain consecutive repeated elements.

  • 'monotonic'— Checks if data is in descending or ascending order. Data can contain consecutive repeated elements.

  • 'strictascend'— Checks if data is in strictly ascending order. Data cannot contain duplicate or missing elements.

  • 'strictdescend'— Checks if data is in strictly descending order. Data cannot contain duplicate or missing elements.

  • 'strictmonotonic'— Checks if data is in strictly descending or strictly ascending order. Data cannot contain duplicate or missing elements.

directioncan also be a cell array containing a list of these character vectors, where each element in the list corresponds to a column ofA. For example,issortedrows(A,[2 4],{'ascend' 'descend'})first checks if the rows ofAare in ascending order based on the second column. Then, to break ties,issortedrowschecks if the rows are in descending order based on the fourth column.

Ifcolumnis specified, then the number of elements in the cell array must match the length ofcolumn. Whencolumnis not specified, the cell array must contain an element for every column ofA, or a single element that is applied to all columns.

Data Types:char|cell

Input table, specified as a table or a timetable. Each variable intblAmust be a valid input tosortorsortrows.

Data Types:table|timetable

Name of the first dimension of the input table or timetable, specified as a character vector.

  • IftblAis a table with row names, thenrowDimNameis the name of the first dimension of the table. By default, the name of the first dimension is'Row'. Dimension names are a property of tables. You can access the dimension names oftblAusingtblA.Properties.DimensionNames.

  • IftblAis a timetable, thenrowDimNameis the name of the vector of row times. You can specify its name when you create a timetable, such asTimeorDate. You can also access the dimension names usingtblA.Properties.DimensionNames.

Example:If a tableThas row names, and you changed the name of the first dimension usingT.Properties.DimensionName{1} = 'Name', thenissortedrows(T,'Name')checks if the table is sorted by row names.

Example:If a timetableTThas a time vector namedDate, thenissortedrows(TT,'Date')checks if the timetable is sorted by the dates and times thatDatecontains.

Data Types:char

Sorting variables, specified as a scalar integer, a vector of integers, a variable name, a cell array of variable names, or a logical vector.varsindicates the table variables to sort by.

If an element ofvarsis a positive integer, thenissortedrowschecks if the rows in the corresponding variable intblAare in ascending order. If an element ofvarsis a negative integer, thenissortedrowschecks if the rows in the corresponding variable intblAare in descending order.

Example:issortedrows(tblA,{'Height','Weight'})checks if the rows oftblAare in ascending order, first by the variableHeight, then by the variableWeightto break ties.

Example:issortedrows(tblA,[1 4])首先检查是否按升序或表中的行der based on the first variable, then breaks ties by checking if the rows are in ascending order based on the fourth variable.

Example:issortedrows(TT,{'Time','X'})checks if the row times of a timetable are in ascending order, then breaks ties by checking if the rows are in ascending order based on the table variable'X'.

Data Types:double|single|char|cell|logical

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:issortedrows(A,'MissingPlacement','last')

Placement of missing values (NaN,NaT,, andmissing) specified as the comma-separated pair consisting of'MissingPlacement'and one of the following:

  • 'auto'— Missing elements are required to be placed last for ascending order and first for descending order to return 1.

  • 'first'— Missing elements are required to be placed first to return 1.

  • 'last'— Missing elements are required to be placed last to return 1.

Element comparison method, specified as the comma-separated pair consisting of'ComparisonMethod'and one of the following:

  • 'auto'— Check if the rows ofAare sorted byreal(A)whenAis real, and check if the rows ofAare sorted byabs(A)whenAis complex.

  • 'real'— Check if the rows ofAare sorted byreal(A)whenA是真实的或复杂的。如果列elements with consecutive equal real parts, then checkimag(A)to break ties.

  • 'abs'— Check if the rows ofAare sorted byabs(A)whenA是真实的或复杂的。如果列elements with consecutive equal magnitude, then checkangle(A)in the interval (-π,π] to break ties.

Extended Capabilities

Introduced in R2017a