Accelerating the pace of engineering and science

• Trials

# sub2ind

Convert subscripts to linear indices

## Syntax

linearInd = sub2ind(matrixSize, rowSub, colSub)
linearInd = sub2ind(arraySize, dim1Sub, dim2Sub, dim3Sub, ...)

## Description

linearInd = sub2ind(matrixSize, rowSub, colSub) returns the linear index equivalents to the row and column subscripts rowSub and colSub for a matrix of size matrixSize. The matrixSize input is a 2-element vector that specifies the number of rows and columns in the matrix as [nRows, nCols]. The rowSub and colSub inputs are positive, whole number scalars or vectors that specify one or more row-column subscript pairs for the matrix. Example 3 demonstrates the use of vectors for the rowSub and colSub inputs.

linearInd = sub2ind(arraySize, dim1Sub, dim2Sub, dim3Sub, ...) returns the linear index equivalents to the specified subscripts for each dimension of an N-dimensional array of size arraySize. The arraySize input is an n-element vector that specifies the number of dimensions in the array. The dimNSub inputs are positive, whole number scalars or vectors that specify one or more row-column subscripts for the matrix.

All subscript inputs can be single, double, or any integer type. The linearInd output is always of class double.

If needed, sub2ind assumes that unspecified trailing subscripts are 1. See Example 2, below.

## Examples

### Example 1

This example converts the subscripts (2, 1, 2) for three-dimensional array A to a single linear index. Start by creating a 3-by-4-by-2 array A:

```rng(0,'twister');   % Initialize random number generator.
A = rand(3, 4, 2)

A(:,:,1) =
0.8147    0.9134    0.2785    0.9649
0.9058    0.6324    0.5469    0.1576
0.1270    0.0975    0.9575    0.9706
A(:,:,2) =
0.9572    0.1419    0.7922    0.0357
0.4854    0.4218    0.9595    0.8491
0.8003    0.9157    0.6557    0.9340```

Find the linear index corresponding to (2, 1, 2):

```linearInd = sub2ind(size(A), 2, 1, 2)
linearInd =
14
```

Make sure that these agree:

```A(2, 1, 2)            A(14)
ans =                 and =
0.4854               0.4854
```

### Example 2

Using the 3-dimensional array A defined in the previous example, specify only 2 of the 3 subscript arguments in the call to sub2ind. The third subscript argument defaults to 1.

The command

```linearInd = sub2ind(size(A), 2, 4)
ans =
11
```

is the same as

```linearInd = sub2ind(size(A), 2, 4, 1)
ans =
11
```

### Example 3

Using the same 3-dimensional input array A as in Example 1, accomplish the work of five separate sub2ind commands with just one.

Replace the following commands:

```sub2ind(size(A), 3, 3, 2);
sub2ind(size(A), 2, 4, 1);
sub2ind(size(A), 3, 1, 2);
sub2ind(size(A), 1, 3, 2);
sub2ind(size(A), 2, 4, 1);
```

with a single command:

```sub2ind(size(A), [3 2 3 1 2], [3 4 1 3 4], [2 1 2 2 1])
ans =
21    11    15    19    11```

Verify that these linear indices access the same array elements as their subscripted counterparts:

```[A(3,3,2),   A(2,4,1),   A(3,1,2),   A(1,3,2),   A(2,4,1)]
ans =
0.6557    0.1576    0.8003    0.7922    0.1576

A([21, 11, 15, 19, 11])
ans =
0.6557    0.1576    0.8003    0.7922    0.1576```