ind2sub - Convert linear indices to subscripts - MATLAB (original) (raw)

Convert linear indices to subscripts

Syntax

Description

[[row](#mw%5F4106e0b9-8475-45ee-83b4-1e654eeb36c3),[col](#mw%5F97089cd4-2fb9-44f4-8f41-57c3396c39d5)] = ind2sub([sz](#mw%5F0bf5b131-8d65-4dfc-a28c-47f87d08c011),[ind](#mw%5F564f4b8f-0f86-437b-9286-4c1217d846f7)) returns the arrays row and col containing the equivalent row and column subscripts corresponding to the linear indicesind for a matrix of size sz. Heresz is a vector with two elements, where sz(1) specifies the number of rows and sz(2) specifies the number of columns.

example

[[I1,I2,...,In](#mw%5F2ed6f7e0-3547-4867-928e-261b5fc0b058)] = ind2sub([sz](#mw%5F0bf5b131-8d65-4dfc-a28c-47f87d08c011),[ind](#mw%5F564f4b8f-0f86-437b-9286-4c1217d846f7)) returns n arrays I1,I2,...,In containing the equivalent multidimensional subscripts corresponding to the linear indicesind for a multidimensional array of size sz. Heresz is a vector with n elements that specifies the size of each array dimension.

example

Examples

collapse all

Convert the linear indices [3 4 5 6] to row and column subscripts in a 3-by-3 matrix. The mapping from linear indices to subscripts (indexing by position) is illustrated in the following.

Create input vectors and perform the conversion.

ind = [3 4 5 6]; sz = [3 3]; [row,col] = ind2sub(sz,ind)

Convert the linear indices [3 4 5 6] to subscripts in a 2-by-2-by-2 array. The mapping from linear indices to subscripts (indexing by position) for a 2-by-2-by-2 array can be illustrated as in the following.

Create input vectors and perform the conversion.

ind = [3 4 5 6]; sz = [2 2 2]; [I1,I2,I3] = ind2sub(sz,ind)

Convert a linear index of a 3-D array to a subscript index.

Create an array, and find the subscript index corresponding to the 14 th element of the array.

A = rand(3,4,2); [row,col,page] = ind2sub(size(A),14)

Check that both index versions refer to the same element of the array.

When using ind2sub for an N-dimensional array, you would typically supply N output arguments for each dimension of the matrix. This example shows the different results when you return fewer output arguments for a 3-dimensional array.

Create the input arguments needed to convert the linear indices 1 through 8 for a 3-dimensional array with size 2-by-2-by-2.

Specify three output arguments when using ind2sub to return the row, column, and page subscripts for the 2-by-2-by-2 array.

[row,col,page] = ind2sub(sz,ind)

row = 1×8

 1     2     1     2     1     2     1     2

col = 1×8

 1     1     2     2     1     1     2     2

page = 1×8

 1     1     1     1     2     2     2     2

If you specify only two output arguments, ind2sub ignores the third dimension of the array and returns subscripts for a 2-dimensional array with size 2-by-4 instead.

[row,col] = ind2sub(sz,ind)

row = 1×8

 1     2     1     2     1     2     1     2

col = 1×8

 1     1     2     2     3     3     4     4

If you specify only one output argument, ind2sub ignores the second and third dimensions of the array and returns subscripts for a 1-dimensional array with size 1-by-8 instead.

row = 1×8

 1     2     3     4     5     6     7     8

When converting linear indices to subscript indices using a target matrix size that is smaller than required, the ind2sub function automatically expands the matrix size as necessary.

For example, define linear indices that are outside the matrix size of 3-by-1.

Convert these indices to subscript indices for a 3-by-1 matrix.

[row,col] = ind2sub([3 1],ind)

ind2sub does not produce an error for this operation. Instead, ind2sub automatically adds columns and expands the matrix size to 3-by-5 to accommodate the conversion of the specified linear indices to subscript indices.

For comparison, convert the same linear indices to subscript indices for a 3-by-5 matrix. The resulting subscript indices for a 3-by-5 matrix and a 3-by-1 matrix are the same.

[row,col] = ind2sub([3 5],ind)

Input Arguments

collapse all

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

Linear indices, specified as a scalar, vector, matrix, or multidimensional array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Row subscripts, returned as a scalar, vector, matrix, or multidimensional array. The size of row is the same as the size of the inputind.

Data Types: double

Column subscripts, returned as a scalar, vector, matrix, or multidimensional array. The size of col is the same as the size of the inputind.

Data Types: double

Multidimensional subscripts, returned as a scalar, vector, matrix, or multidimensional array. The size of each array I1,I2,…,In is the same as the size of the input ind.

Data Types: double

Tips

Algorithms

For an array A, if [I1,…,In] = ind2sub(size(A),ind), then A(I1(k),…,In(k)) = A(ind(k)) for all k.

Extended Capabilities

expand all

Theind2sub function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

The ind2sub 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

expand all

The input argument for the size of the array must be a vector of positive integers with two or more elements. In previous releases, ind2sub(sz,ind) treated a scalar input size sz as the number of rows in the array and assumed the size of the other dimensions to be 1. For example:

[row,col] = ind2sub(6,[2 3 4])

Starting in R2024b, a scalar input forsz results in an error. Instead, you must specify sz as a vector of positive integers with two or more elements. For example:

[row,col] = ind2sub([6 1],[2 3 4])