issortedrows

Tests whether the rows of a matrix are sorted.

Category: Language Fundamentals > Matrices and Arrays

Syntax

tf = issortedrows(A)

True when the rows are in ascending order, comparing column 1 first, then column 2 on ties, and so on.

tf = issortedrows(A, col)

Compares the listed columns; a negative index means descending for that column.

tf = issortedrows(A, direction)

Applies 'ascend' or 'descend' to all columns.

Description

Rows are compared like words in a dictionary: the first column decides, and later columns only break ties. The result tells whether sortrows(A) would leave the matrix unchanged, without doing the sort.

Input arguments

A
Numeric matrix or table.
col
Vector of column numbers, with sign giving the direction.
direction
'ascend', 'descend' or 'monotonic'.

Output arguments

tf
Logical scalar.

Example

A = [1 1; 1 2; 2 0];
issortedrows(A)
issortedrows([1 2; 1 1])
issortedrows([1 2; 1 1], [1 -2])
Output
ans = logical
   1
ans = logical
   0
ans = logical
   1

See also

sortrows issorted sort unique