sub2ind - Convert subscripts to linear indices - MATLAB (original) (raw)
Convert subscripts to linear indices
Syntax
Description
[ind](#mw%5F5260ba83-8e04-4ee8-a636-c078dd40ef98) = sub2ind([sz](#mw%5Fddeff116-73c4-4c56-87a4-1a31fe0006fa),[row](#mw%5F54c1f6bc-22a7-4810-aa59-2d564d5173e9),[col](#mw%5F4ca15746-0835-4994-ae43-6c013ab0e04a))
returns the linear indices ind
corresponding to the row and column subscripts in row
and col
for a matrix of sizesz
. Here sz
is a vector with two elements, wheresz(1)
specifies the number of rows and sz(2)
specifies the number of columns.
[ind](#mw%5F5260ba83-8e04-4ee8-a636-c078dd40ef98) = sub2ind([sz](#mw%5Fddeff116-73c4-4c56-87a4-1a31fe0006fa),[I1,I2,...,In](#mw%5F20e89540-0f9a-4754-bd27-716ed64497c7))
returns the linear indices ind
corresponding to multidimensional subscripts in n
arrays I1,I2,...,In
for a multidimensional array of size sz
. Here sz
is a vector with n
elements that specifies the size of each array dimension.
Examples
The mapping from subscripts (indexing by position) to linear indices for a 3-by-3 matrix can be illustrated as in the following.
Specify the row and column subscripts in a 3-by-3 matrix. Convert the subscripts to linear indices.
row = [1 2 3 1]; col = [2 2 2 3]; sz = [3 3]; ind = sub2ind(sz,row,col)
The mapping from subscripts to linear indices for a 2-by-2-by-2 array can be illustrated as in the following.
Specify the row, column, and page subscripts in a 2-by-2-by-2 array. Convert the subscripts to linear indices.
I1 = [1 2 1 2]; I2 = [2 2 1 1]; I3 = [1 1 2 2]; sz = [2 2 2]; ind = sub2ind(sz,I1,I2,I3)
Convert a subscript index of a 3-D array to a single linear index.
Create an array, and find the linear index corresponding to the element in the (2,1,2) position.
A = rand(3,4,2); linearInd = sub2ind(size(A),2,1,2)
Check that both index versions refer to the same element.
Input Arguments
Size of array, specified as a vector of positive integers. Each element of this vector indicates the size of the corresponding dimension. For example, [2 3 4]
defines a 2-by-3-by-4 array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Row subscripts, specified as a scalar, vector, matrix, or multidimensional array.row
and col
can be arrays of the same size, or either one can be scalar.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Column subscripts, specified as a scalar, vector, matrix, or multidimensional array.row
and col
can be arrays of the same size, or either one can be scalar.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Multidimensional subscripts, specified as a scalar, vector, matrix, or multidimensional array. I1,I2,…,In
can be arrays of the same size, or any of them can be scalar.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Output Arguments
Linear indices, returned as a scalar, vector, matrix, or multidimensional array.
- If the subscript inputs all have the same size, then
ind
is also that size. - If the subscript inputs are a mix of scalars and arrays, then
ind
has the size of the nonscalar subscript inputs.
Data Types: double
Algorithms
For an array A
, if ind = sub2ind(size(A),I1,…,In)
, then A(ind(k)) = A(I1(k),…,In(k))
for all k
.
Extended Capabilities
Thesub2ind
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- The input vectors
sz
,row
, andcol
can contain only finite integer values. - The input vector
sz
must be fixed size at code generation time. - For code generation, the maximum number of elements in an array is constrained by the code generator and the target hardware. See Array Size Restrictions for Code Generation (MATLAB Coder). The size vector
sz
is not supported for arrays with more than the maximum number of elements.
Usage notes and limitations:
- The first argument must be a valid size vector. Code generation does not support size vectors for arrays with more than intmax elements.
The sub2ind
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
sub2ind now leverages scalar expansion and accepts a mix of scalars and vectors for subscript inputs. For example,sub2ind(sz,[1 2 3],2)
is now the same as sub2ind(sz,[1 2 3],[2 2 2])
. Previously, the subscript inputs were required to be the same size.