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.2, Nov 26, 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
    • find
    • ind2sub
    • sub2ind

sub2ind — Convert N-D subscripts to one-based column-major linear indices.

sub2ind combines one subscript per dimension of a positive integral size vector using checked column-major strides.

Syntax

ind = sub2ind(sz, I1, In...)

Inputs

sz
Type
SizeArg
Required
Yes
Description
Size vector describing source array dimensions.
I1
Type
Any
Required
Yes
Description
First-dimension subscript values.
In
Type
Any
Required
Variadic
Description
Remaining per-dimension subscript arrays/scalars.
NameTypeRequiredDefaultDescription
szSizeArgYes—Size vector describing source array dimensions.
I1AnyYes—First-dimension subscript values.
InAnyVariadic—Remaining per-dimension subscript arrays/scalars.

Returns

ind
Type
NumericArray
Description
Column-major linear indices corresponding to provided subscripts.
NameTypeDescription
indNumericArrayColumn-major linear indices corresponding to provided subscripts.

Errors

RunMat:sub2ind:InvalidInput
When
Size vector, subscript count, or subscript types are invalid.
Message
sub2ind: invalid input arguments
RunMat:sub2ind:IndexBounds
When
At least one subscript lies outside bounds for its dimension.
Message
sub2ind: subscript index exceeds dimension bounds
RunMat:sub2ind:ProviderError
When
GPU provider sub2ind hook fails.
Message
sub2ind: provider execution failed
RunMat:sub2ind:InternalError
When
Internal tensor conversion/output construction fails.
Message
sub2ind: internal error
IdentifierWhenMessage
RunMat:sub2ind:InvalidInputSize vector, subscript count, or subscript types are invalid.sub2ind: invalid input arguments
RunMat:sub2ind:IndexBoundsAt least one subscript lies outside bounds for its dimension.sub2ind: subscript index exceeds dimension bounds
RunMat:sub2ind:ProviderErrorGPU provider sub2ind hook fails.sub2ind: provider execution failed
RunMat:sub2ind:InternalErrorInternal tensor conversion/output construction fails.sub2ind: internal error

Shape and bounds

All non-scalar subscript arrays must share their shape; scalars expand to that shape. The result is a double scalar or an array matching the common non-scalar shape. Every subscript must be a positive integer within its dimension bound.

The number of subscript inputs must match the size-vector length. Invalid dimensions and noninteger, complex, nonfinite, or out-of-bounds subscripts produce typed errors.

Exact integer and resident behavior

The size vector and each subscript accept all eight native integer classes, single, and double. Native integer inputs are checked from authoritative storage, including values above flintmax; the double result must remain exact.

A provider hook handles supported resident floating-point forms. Native integer resident values take an exact owner-aware gather path; successful output is restored to the same owner when it can represent binary64. No fusion occurs.

Examples

Compile vector indices and bounds errors

dims = [3 5]; idx = sub2ind(dims,[1;2;3],3); badrow = 4; caught = false; try; bad = sub2ind(dims,badrow,1); catch failure; caught = strcmp(failure.identifier,'RunMat:sub2ind:IndexBounds'); end

Reject a subscript beyond its dimension

idx = sub2ind([2 3], 3, 1)

Map a matrix coordinate

idx = sub2ind([3 4], 2, 3)

Expected output:

idx = 8

Preserve vector orientation

idx = sub2ind([3 5], [1;2;3], [3;3;3])

Expected output:

idx = [7;8;9]

Map higher-dimensional coordinates

idx = sub2ind([2 3 4], [1 1], [2 3], [1 2])

Expected output:

idx = [3 11]

Using sub2ind with coding agents

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

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

FAQ

Are fractional subscripts rounded?⌄

No. Fractional, nonfinite, and complex subscripts reject.

Do scalar subscripts broadcast?⌄

Yes, to the common shape of the non-scalar subscripts.

Related Array functions

Indexing

find · ind2sub

Accumulation

accumarray

Sorting Sets

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

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

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 sub2ind is executed, line by line, in Rust.

  • View the source for sub2ind 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
  • Shape and bounds
  • Exact integer and resident behavior
  • Examples
  • Compile vector indices and bounds errors
  • Reject a subscript beyond its dimension
  • Map a matrix coordinate
  • Preserve vector orientation
  • Map higher-dimensional coordinates
  • Using sub2ind with coding agents
  • FAQ
  • Related Array functions
  • Indexing
  • Accumulation
  • Sorting Sets
  • Shape
  • Creation
  • Combinatorics
  • Binning
  • Grouping
  • Introspection
  • Source you can read
  • About RunMat