Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Subscripts from linear index

`[I,J] = ind2sub(siz,IND)`

[I1,I2,I3,...,In] = ind2sub(siz,IND)

The `ind2sub`

function determines the equivalent subscript values
corresponding to a single index into an array.

`[I,J] = ind2sub(siz,IND)`

returns
the matrices `I`

and `J`

containing
the equivalent row and column subscripts corresponding to each linear
index in the matrix `IND`

for a matrix of size `siz`

. `siz`

is
a vector with `ndim(A)`

elements (in this case, 2),
where `siz(1)`

is the number of rows and `siz(2)`

is
the number of columns.

For matrices, `[I,J] = ind2sub(size(A),find(A>5))`

returns
the same values as `[I,J] = find(A>5)`

.

`[I1,I2,I3,...,In] = ind2sub(siz,IND)`

returns `n`

subscript
arrays `I1`

,`I2`

,`...`

,`In`

containing
the equivalent multidimensional array subscripts equivalent to `IND`

for
an array of size `siz`

. `siz`

is
an `n`

-element vector that specifies the size of
each array dimension.

The `IND`

input can be `single`

, `double`

,
or any integer type. The outputs are always of class `double`

.

The mapping from linear indexes to subscript equivalents for a 3-by-3 matrix is

This code determines the row and column subscripts in a 3-by-3 matrix, of elements with linear indices 3, 4, 5, 6.

IND = [3 4 5 6] s = [3,3]; [I,J] = ind2sub(s,IND) I = 3 1 2 3 J = 1 2 2 2

The mapping from linear indexes to subscript equivalents for a 2-by-2-by-2 array is

This code determines the subscript equivalents in a 2-by-2-by-2
array, of elements whose linear indices 3, 4, 5, 6 are specified in
the `IND`

matrix.

IND = [3 4;5 6]; s = [2,2,2]; [I,J,K] = ind2sub(s,IND) I = 1 2 1 2 J = 2 2 1 1 K = 1 1 2 2

When calling `ind2sub`

for an N-dimensional
matrix, you would typically supply N output arguments in the call:
one for each dimension of the matrix. This example shows what happens
when you return three, two, and one output when calling `ind2sub`

on
a 3-dimensional matrix.

The matrix is 2-by-2-by-2 and the linear indices are 1 through 8:

dims = [2 2 2]; indices = [1 2 3 4 5 6 7 8];

The 3-output call to `ind2sub`

returns the
expected subscripts for the 2-by-2-by-2 matrix:

[rowsub colsub pagsub] = ind2sub(dims, indices) rowsub = 1 2 1 2 1 2 1 2 colsub = 1 1 2 2 1 1 2 2 pagsub = 1 1 1 1 2 2 2 2

If you specify only two outputs (row and column), `ind2sub`

still
returns a subscript for each specified index, but drops the third
dimension from the matrix, returning subscripts for a 2-dimensional,
2-by-4 matrix instead:

[rowsub colsub] = ind2sub(dims, indices) rowsub = 1 2 1 2 1 2 1 2 colsub = 1 1 2 2 3 3 4 4

If you specify one output (row), `ind2sub`

drops
both the second and third dimensions from the matrix, and returns
subscripts for a 1-dimensional, 1-by-8 matrix instead:

[rowsub] = ind2sub(dims, indices) rowsub = 1 2 3 4 5 6 7 8

Was this topic helpful?