issorted

Tests whether the elements of a vector are in sorted order.

Category: Language Fundamentals > Matrices and Arrays

Syntax

tf = issorted(A)

True when A is in ascending order (equal neighbours allowed).

tf = issorted(A, direction)

Checks a specific order: 'ascend', 'descend', 'monotonic', 'strictascend', 'strictdescend' or 'strictmonotonic'.

Description

It is a cheap check that is worth doing before a function that assumes sorted input, such as interp1 or a binary search. Checking is a single pass, so it is faster than sorting and comparing.

NaN values count as the largest in ascending order, so they are expected at the end.

Input arguments

A
Numeric or logical vector.
direction
Text naming the order to test; the strict variants forbid equal neighbours.

Output arguments

tf
Logical scalar.

Example

issorted([1 2 2 5])
issorted([1 2 2 5], 'strictascend')
issorted([3 1 2])
Output
ans = logical
   1
ans = logical
   0
ans = logical
   0

See also

sort issortedrows sortrows isuniform