issorted — Test whether arrays are sorted in MATLAB and RunMat.
issorted(A) tests whether data is already sorted. Dimension, order direction, strictness, missing placement, and row-wise modes follow MATLAB semantics.
Syntax
tf = issorted(A)
tf = issorted(A, arg1)
tf = issorted(A, arg1, arg2)
tf = issorted(A, ..., "ComparisonMethod", method)
tf = issorted(A, ..., "MissingPlacement", placement)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input array. |
arg1 | Any | Yes | — | Dimension selector or direction token (including 'rows'). |
arg2 | Any | Yes | — | Direction token or additional mode selector. |
arg | Any | Variadic | — | Optional dimension/direction/rows arguments. |
name | StringScalar | Yes | "ComparisonMethod" | Name-value option key. |
method | StringScalar | Yes | "auto" | Comparison method: 'auto', 'real', or 'abs'. |
name | StringScalar | Yes | "MissingPlacement" | Name-value option key. |
placement | StringScalar | Yes | "auto" | Missing placement policy: 'auto', 'first', or 'last'. |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | True when data is already sorted by requested criteria. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:issorted:RowsRequiresTwoDimensionalInput | 'rows' mode is used with non-2D input. | issorted: 'rows' expects a 2-D matrix |
RunMat:issorted:StringComparisonMethodUnsupported | ComparisonMethod is used with string arrays. | issorted: 'ComparisonMethod' is not supported for string arrays |
RunMat:issorted:DuplicateDirection | Multiple direction tokens are provided. | issorted: sorting direction specified more than once |
RunMat:issorted:InvalidArgument | Parser encounters invalid or unrecognized option/value arguments. | issorted: invalid argument sequence |
RunMat:issorted:InvalidInput | Input type cannot be normalized into a supported sortable representation. | issorted: invalid input type |
RunMat:issorted:Internal | Internal conversion/allocation paths fail. | issorted: internal operation failed |
How issorted works
issorted(A)examines the first non-singleton dimension and returns a logical scalar.issorted(A, dim)selects the dimension explicitly (1-based).- Direction flags include
'ascend','descend','monotonic','strictascend','strictdescend', and'strictmonotonic'. - Name-value options mirror MATLAB:
'MissingPlacement'accepts'auto','first', or'last'.'ComparisonMethod'accepts'auto','real', or'abs'for numeric and complex data.issorted(A,'rows', ...)verifies lexicographic ordering of rows.- Logical and character arrays are promoted to double precision for comparison; string arrays are compared lexicographically, recognising the literal
<missing>token as a missing element. - Empty arrays, scalars, and singleton dimensions are always reported as sorted.
Does RunMat run issorted on the GPU?
issorted is registered as a sink builtin. When the input tensor lives on the GPU the runtime gathers it to host memory and performs the check there, guaranteeing MATLAB-compatible semantics.
Future providers may implement a predicate hook so that simple monotonic checks can execute entirely on device. Until then the behaviour is identical for CPU and GPU arrays.
The result is a logical scalar (true or false) regardless of input residency.
Examples
Checking an ascending vector
A = [5 12 33 39 78 90 95 107];
tf = issorted(A)Expected output:
tf =
1Verifying strict increase
B = [1 1 2 3];
tf = issorted(B, 'strictascend')Expected output:
tf =
0Using a specific dimension
C = [1 4 2; 3 6 5];
tf = issorted(C, 2, 'descend')Expected output:
tf =
0Allowing either ascending or descending order
D = [10 8 8 5 1];
tf = issorted(D, 'monotonic')Expected output:
tf =
1Controlling missing placements
E = [NaN NaN 4 7];
tf = issorted(E, 'MissingPlacement', 'first')Expected output:
tf =
0Comparing complex data by magnitude
Z = [1+1i, 2+3i, 4+0i];
tf = issorted(Z, 'ComparisonMethod', 'abs')Expected output:
tf =
1Checking row order
R = [1 2 3; 1 2 4; 2 0 1];
tf = issorted(R, 'rows')Expected output:
tf =
1Working with string arrays
str = [ "apple" "banana"; "apple" "carrot" ];
tf = issorted(str, 2)Expected output:
tf =
0Using issorted with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how issorted changes the result.
Run a small issorted example, explain the result, then change one input and compare the output.
FAQ
Does issorted modify its input?⌄
No. The function inspects the data in-place and returns a logical scalar.
What is the difference between 'ascend' and 'strictascend'?⌄
'ascend' allows consecutive equal values, while 'strictascend' requires every element to be strictly greater than its predecessor and rejects missing values.
How does 'monotonic' work?⌄
'monotonic' succeeds when the data is entirely non-decreasing *or* non-increasing. Use 'strictmonotonic' to forbid repeated or missing values.
Can I control where NaN values appear?⌄
Yes. 'MissingPlacement','first' requires missing values to precede finite ones, 'last' requires them to trail, and 'auto' follows MATLAB’s default (end for ascending checks, start for descending).
Is 'ComparisonMethod' relevant for real data?⌄
For real data 'auto' and 'real' are identical. 'abs' compares magnitudes first and breaks ties using the signed value, matching MATLAB.
Does the function support GPU arrays?⌄
Yes. GPU inputs are gathered automatically and the result is computed on the host to guarantee correctness until dedicated provider hooks are available.
How are string arrays treated?⌄
Strings are compared lexicographically using Unicode code-point order. The literal <missing> is treated as a missing value so 'MissingPlacement' rules apply.
What about empty arrays or dimensions of length 0?⌄
Empty slices are considered sorted. Passing a dimension larger than ndims(A) also returns true.
Related Array functions
Shape
cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how issorted is executed, line by line, in Rust.
- View the source for issorted in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.