# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# 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.

```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.

## Examples

collapse all

Convert an index of a 3-D array to a single linear index.

Create an array `A`, 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)```
```linearInd = 14 ```

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

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