RunMat
  • Pricing
RunMat
GitHub
GitHub
Sign inTry in browser
DesktopRuntimePhysicsCADAdministrationAgent

Get RunMat updates

New releases, guides and examples.

Product+
  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download
Math+
  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting
Physics+
  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA
Industries+
  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance
Learn+
  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students
Compare+
  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons
Company+
  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms

Product

  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download

Math

  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting

Physics

  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA

Industries

  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance

Learn

  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students

Compare

  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons

Company

  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms
GitHubXLinkedIn

© Dystr, Inc. · RunMat v0.7.1, Nov 17, 2026

RunMat® is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
See all docs
Builtin Reference
    • argsort
    • intersect
    • ismember
    • ismembertol
    • issorted
    • issortedrows
    • setdiff
    • setxor
    • sort
    • sortrows
    • union
    • unique

argsort — Return one-based permutation indices using RunMat sort options.

argsort(X) returns the permutation indices that order X the same way sort(X) would. This RunMat convenience operation uses the index behavior of [~, I] = sort(X, ...) for supported dimensions and directions.

Syntax

I = argsort(A)
I = argsort(A, arg1)
I = argsort(A, arg1, arg2)
I = argsort(A, ..., "ComparisonMethod", method)
I = argsort(A, ..., "MissingPlacement", placement)

Inputs

A
Type
Any
Required
Yes
Description
Input array.
arg1
Type
Any
Required
Yes
Description
Dimension selector or direction token.
arg1
Type
Any
Required
Yes
Description
Dimension selector, placeholder, or direction token.
arg2
Type
Any
Required
Yes
Description
Dimension selector or direction token.
arg
Type
Any
Required
Variadic
Description
Optional dimension/direction arguments.
name
Type
StringScalar
Required
Yes
Default
"ComparisonMethod"
Description
Name-value option key.
method
Type
StringScalar
Required
Yes
Default
"auto"
Description
Comparison method: 'auto', 'real', or 'abs'.
name
Type
StringScalar
Required
Yes
Default
"MissingPlacement"
Description
Name-value option key.
placement
Type
StringScalar
Required
Yes
Default
"auto"
Description
Requested NaN placement option (currently unsupported).
NameTypeRequiredDefaultDescription
AAnyYes—Input array.
arg1AnyYes—Dimension selector or direction token.
arg1AnyYes—Dimension selector, placeholder, or direction token.
arg2AnyYes—Dimension selector or direction token.
argAnyVariadic—Optional dimension/direction arguments.
nameStringScalarYes"ComparisonMethod"Name-value option key.
methodStringScalarYes"auto"Comparison method: 'auto', 'real', or 'abs'.
nameStringScalarYes"MissingPlacement"Name-value option key.
placementStringScalarYes"auto"Requested NaN placement option (currently unsupported).

Returns

I
Type
NumericArray
Description
One-based permutation indices that sort each slice.
NameTypeDescription
INumericArrayOne-based permutation indices that sort each slice.

Errors

RunMat:sort:InvalidDimension
When
Dimension argument is non-positive, non-integer, or otherwise invalid.
Message
sort: invalid dimension argument
RunMat:sort:ComparisonMethodRequiresString
When
ComparisonMethod option value is not string-like.
Message
sort: 'ComparisonMethod' requires a string value
RunMat:sort:ComparisonMethodUnknown
When
ComparisonMethod option value is not one of 'auto'/'real'/'abs'.
Message
sort: unsupported ComparisonMethod
IdentifierWhenMessage
RunMat:sort:InvalidDimensionDimension argument is non-positive, non-integer, or otherwise invalid.sort: invalid dimension argument
RunMat:sort:ComparisonMethodRequiresStringComparisonMethod option value is not string-like.sort: 'ComparisonMethod' requires a string value
RunMat:sort:ComparisonMethodUnknownComparisonMethod option value is not one of 'auto'/'real'/'abs'.sort: unsupported ComparisonMethod
RunMat:sort:MissingPlacementRequiresStringMissingPlacement option value is not string-like.sort: 'MissingPlacement' requires a string value
RunMat:sort:MissingPlacementUnknownMissingPlacement option value is not one of 'auto'/'first'/'last'.sort: unsupported MissingPlacement
RunMat:sort:InvalidArgumentParser encounters invalid or unrecognized option/value arguments.sort: invalid argument sequence

Behavior

Operates along the first non-singleton dimension by default. Pass a dimension argument to override.

Accepts the same direction keywords as sort: 'ascend' (default) or 'descend'.

Supports 'ComparisonMethod' values 'auto', 'real', and 'abs' for real and complex inputs.

Returns indices as double-precision tensors using MATLAB's one-based indexing.

Treats NaN values as missing: they appear at the end for ascending permutations and at the beginning for descending permutations.

Acts as a residency sink. GPU tensors are gathered when the active provider does not expose a specialised sort kernel.

Types, placement and limitations

argsort publishes only one-based double permutation indices. It currently uses the same evaluation as sort, including computing sorted values. Resident indices are restored to the input owner; a provider hook may stage work through host memory.

Resident execution

These operations are eager fusion boundaries, not elementwise kernels or reductions, and do not broadcast. Provider availability does not imply a device-native sorting or set kernel: current providers may stage host work. An unsupported input can use exact host fallback before provider work starts. Errors after an attempt, including cancellation, are returned without repeating the operation on the host. Supported resident outputs return to their owning provider; predicates and all-indices cells have the host placement described above. Distributed execution is not supported.

Examples

Getting indices that sort a vector

A = [4; 1; 3];
idx = argsort(A)

Expected output:

idx =
     2
     3
     1

Reordering data with the permutation indices

A = [3 9 1 5];
idx = argsort(A);
sorted = A(idx)

Expected output:

sorted =
     1     3     5     9

Sorting along a specific dimension

A = [1 6 4; 2 3 5];
idx = argsort(A, 2)

Expected output:

idx =
     1     3     2
     1     2     3

Descending order permutations

A = [10 4 7 9];
idx = argsort(A, 'descend')

Expected output:

idx =
     1     4     3     2

Using ComparisonMethod to sort by magnitude

A = [-8 -1 3 -2];
idx = argsort(A, 'ComparisonMethod', 'abs')

Expected output:

idx =
     2     4     3     1

Handling NaN values during permutation

A = [NaN 4 1 2];
idx = argsort(A)

Expected output:

idx =
     3     4     2     1

Argsort on GPU tensors falls back gracefully

G = gpuArray(randn(5, 1));
idx = argsort(G)

Using argsort with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how argsort changes the result.

Run a small argsort example, explain the result, then change one input and compare the output.

FAQ

How is argsort different from sort?⌄

argsort returns only the permutation indices. It behaves like calling [~, I] = sort(X, ...) and publishes only the indices; the current evaluation also computes sorted values.

Are the indices one-based like MATLAB?⌄

Yes. All indices follow MATLAB's one-based convention so they can be used directly with subsequent indexing operations.

Does argsort support the same arguments as sort?⌄

Yes. Dimension arguments, direction keywords, and 'ComparisonMethod' behave exactly like they do for sort.

How are NaN values ordered?⌄

NaNs are treated as missing. They appear at the end for ascending permutations and at the beginning for descending permutations, matching MATLAB.

Can I call argsort on GPU arrays?⌄

argsort publishes only one-based double permutation indices. It currently uses the same evaluation as sort, including computing sorted values. Resident indices are restored to the input owner; a provider hook may stage work through host memory. These operations are eager fusion boundaries, not elementwise kernels or reductions, and do not broadcast. Provider availability does not imply a device-native sorting or set kernel: current providers may stage host work. An unsupported input can use exact host fallback before provider work starts. Errors after an attempt, including cancellation, are returned without repeating the operation on the host. Supported resident outputs return to their owning provider; predicates and all-indices cells have the host placement described above. Distributed execution is not supported.

Is the permutation stable?⌄

Yes. Equal elements keep their relative order so that argsort remains consistent with MATLAB's stable sorting semantics.

What type is returned?⌄

A double-precision tensor (or scalar) with the same shape as the input, containing permutation indices.

Does argsort mutate its input?⌄

No. It only returns indices. Combine the result with indexing (A(idx)) to obtain reordered values when needed.

Related Array functions

Sorting Sets

intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique

Accumulation

accumarray

Shape

blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat

Creation

colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros

Combinatorics

combinations · nchoosek · perms

Binning

discretize

Indexing

find · ind2sub · sub2ind

Grouping

findgroups · groupcounts · grp2idx · splitapply

Introspection

height · iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · size · width

Source you can read

Every RunMat function's source is public. Read exactly how argsort is executed, line by line, in Rust.

  • View the source for argsort in Rust on GitHub
  • Learn how the RunMat runtime works
  • Found a bug? Open an issue with a minimal reproduction.

About RunMat

RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • Behavior
  • Types, placement and limitations
  • Resident execution
  • Examples
  • Getting indices that sort a vector
  • Reordering data with the permutation indices
  • Sorting along a specific dimension
  • Descending order permutations
  • Using ComparisonMethod to sort by magnitude
  • Handling NaN values during permutation
  • Argsort on GPU tensors falls back gracefully
  • Using argsort with coding agents
  • FAQ
  • Related Array functions
  • Sorting Sets
  • Accumulation
  • Shape
  • Creation
  • Combinatorics
  • Binning
  • Indexing
  • Grouping
  • Introspection
  • Source you can read
  • About RunMat