ind2sub — Convert one-based linear indices to N-D subscripts.
ind2sub converts column-major linear indices using a positive integral size vector. Requested output count controls trailing-dimension collapse; an internal call with no requested-output context uses a cell carrier.
Syntax
[I1,...,IN] = ind2sub(sz, ind)Inputs
sz- Type
SizeArg- Required
- Yes
- Description
- Size vector describing source array dimensions.
ind- Type
Any- Required
- Yes
- Description
- Linear indices to convert into per-dimension subscripts.
Returns
I1,...,IN- Type
NumericArray- Description
- Requested double subscript arrays; the final requested output folds trailing dimensions.
Returned values from ind2sub depend on how many outputs the caller requests.
Errors
RunMat:ind2sub:InvalidInput- When
- Size vector or linear index inputs are malformed or unsupported.
- Message
ind2sub: invalid input arguments
RunMat:ind2sub:ProviderError- When
- Provider-side ind2sub execution fails or returns malformed outputs.
- Message
ind2sub: provider execution failed
RunMat:ind2sub:InternalError- When
- Internal tensor/materialization logic fails while building outputs.
- Message
ind2sub: internal error
Dimensions and requested outputs
The size vector contains at least two positive integral extents. Each requested output is a double subscript array. Fewer requested outputs fold trailing dimensions into the final output; with one output this is the original linear index. Extra outputs extend singleton dimensions.
Each output array follows the input index shape, including empty and N-D forms. Positive indices beyond the nominal product extend the final effective dimension, subject to exact structural bounds. An internal callable without a requested-output count retains a cell carrier for all per-dimension outputs.
Numeric and resident behavior
Size extents and indices accept single, double, and all eight native integer classes; wide integers are decoded exactly. Subscripts are doubles. Complex, fractional, nonfinite, and nonpositive values reject.
The binary64 WGPU route can execute admitted forms on device. Other providers use exact owner-aware materialization and restoration where binary64 output is representable. No fusion occurs.
Examples
Compile requested subscripts and errors
sz = [3 4]; indices = [7 8 9]; [row,col] = ind2sub(sz,indices); linear = ind2sub(sz,indices); invalid = [2 0]; caught = false; try; bad = ind2sub(invalid,1); catch failure; caught = strcmp(failure.identifier,'RunMat:ind2sub:InvalidInput'); endReject a zero dimension
I = ind2sub([2 0], 1)Recover row and column
[row,col] = ind2sub([3 4], 8)Expected output:
row = 2, col = 3Convert several indices
[row,col] = ind2sub([3 5], [7 8 9])Expected output:
row = [1 2 3], col = [3 3 3]Collapse dimensions into one output
I = ind2sub([3 4], [1 8])Expected output:
I = [1 8]Using ind2sub with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how ind2sub changes the result.
Run a small ind2sub example, explain the result, then change one input and compare the output.
FAQ
What happens with fewer outputs?⌄
Trailing dimensions collapse into the final requested subscript.
Do output shapes match the input indices?⌄
Yes, including empty arrays and N-D index arrays.
Related Array functions
Accumulation
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
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how ind2sub is executed, line by line, in Rust.
- View the source for ind2sub 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.