Main Content

issorted

Determine if array is sorted

Description

example

TF = issorted(A)returns the logical scalar 1 (true) when the elements ofAare listed in ascending order and 0 (false) otherwise.

  • IfAis a vector, thenissortedreturns 1 when the vector elements are in ascending order.

  • IfAis a matrix, thenissortedreturns 1 when each column ofAis in ascending order.

  • IfAis a multidimensional array, thenissortedreturns 1 whenAis in ascending order along the first dimension whose size does not equal 1.

  • IfAis a timetable, thenissortedreturns 1 when its row time vector is in ascending order. To check the ordering of row times or variables of a timetable with additional options, use theissortedrowsfunction.

example

TF = issorted(A,dim)returns 1 whenAis sorted along dimensiondim。对于example, ifAis a matrix, thenissorted(A,2)returns 1 when each row ofAis in ascending order.

example

TF = issorted(___,direction)returns 1 whenAis sorted in the order specified bydirectionfor any of the previous syntaxes. For example,issorted(A,'monotonic')returns 1 if the elements ofAare ascending or descending.

example

TF = issorted(___,Name,Value)specifies additional parameters for checking sort order. For example,issorted(A,'ComparisonMethod','abs')checks ifAis sorted by magnitude.

TF = issorted(A,'rows')returns 1 when the elements of the first column of a matrix are sorted. If the first column contains repeated elements, thenissortedlooks at the ordering of the second column to determineTF。In general,issortedlooks to the column immediately to the right to determineTFwhen the current and previous columns have repeated elements.

  • IfAis a timetable, thenissortedchecks if the row time vector is in ascending order.

  • This syntax is not supported for a matrix of character vectors.

Note

This syntax is not recommended. Useissortedrowsinstead.

Examples

collapse all

Create a vector and check if it is sorted in ascending order.

A = [5 12 33 39 78 90 95 107]; issorted(A)
ans =logical1

Create a 5-by-5 matrix and check if each row is sorted in descending order.

A = magic(5)
A =5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
issorted(A,2,'descend')
ans =logical0

Sort each row ofAin descending order using thesortfunction, and check that the result has descending rows.

B = sort(A,2,'descend')
B =5×524 17 15 8 1 23 16 14 7 5 22 20 13 6 4 21 19 12 10 3 25 18 11 9 2
issorted(B,2,'descend')
ans =logical1

Create a 2-D array of strings and determine if each column is sorted.

str = ["Horse","Chicken";"cow","Goat"]
str =2x2 string"Horse" "Chicken" "cow" "Goat"
issorted(str)
ans =logical1

Determine if the rows are sorted from left to right.

issorted(str,2)
ans =logical0

Determine if each row is sorted in descending order from left to right.

issorted(str,2,'descend')
ans =logical1

Create a vector containing complex numbers andNaNvalues.

A = [NaN NaN 1+i 1+2i 2+2i 3+i];

Check that theNaNelements are placed first within the vector, and that the remaining elements are sorted by real part.

issorted(A,'MissingPlacement','first','ComparisonMethod','real')
ans =logical1

因为第三和第四的元素Ahave equal real part,issortedchecks if the imaginary part of these elements are also sorted.

imag(A(3))
ans = 1
imag(A(4))
ans = 2

Input Arguments

collapse all

Input array, specified as a vector, matrix, multidimensional array, cell array of character vectors, or timetable.

  • IfAcontains missing values, such asNaN,NaT,, andmissing, then by default,issortedrequires that they are placed at the end to return 1.

  • IfAis complex, then by default,issorteddetermines sort order by the magnitude of the elements. If there are consecutive elements with equal magnitude, thenissortedalso checks the phase angle in the interval (-π, π] to break ties.

  • IfAis a cell array of character vectors or a string array, thenissorteddetermines sort order using the code order for the UTF-16 character encoding scheme. The sort is case-sensitive. For more information on sorted character and string arrays, seeSort Order for Character and String Arrays

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string|cell|分类|datetime|duration|timetable

Complex Number Support:Yes

维操作,指定为是e integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Consider a matrixAissorted(A,1)checks if the data in each column ofAis sorted.

issorted(A,2)checks if the data in each row ofAis sorted.

dimis not supported for timetable input.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Sorting direction, specified as one of the following:

  • 'ascend'— 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.

directionis not supported for timetable input. Useissortedrowsinstead.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:issorted(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.

This name-value pair is not supported for timetable input. Useissortedrowsinstead.

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

  • 'auto'— Check ifAis sorted byreal(A)whenAis real, and check ifAis sorted byabs(A)whenAis complex.

  • 'real'— Check ifAis sorted byreal(A)whenAis real or complex. IfAhas elements with consecutive equal real parts, then checkimag(A)to break ties.

  • 'abs'— Check ifAis sorted byabs(A)whenAis real or complex. IfAhas elements with consecutive equal magnitude, then checkangle(A)in the interval (-π,π] to break ties.

More About

collapse all

Sort Order for Character and String Arrays

MATLAB®stores characters as Unicode®using the UTF-16 character encoding scheme. Character and string arrays are sorted according to the UTF-16 code point order. For the characters that are also the ASCII characters, this order means that uppercase letters come before lowercase letters. Digits and some punctuation also come before letters.

Extended Capabilities

Version History

Introduced before R2006a