sub2ind

Converts subscript to a linear index.

Syntax

ind = sub2ind(dim,i,j)

ind = sub2ind(dim,s1,s2,...,sn)

Inputs

dim
Matrix dimensions.
Type: double | integer
Dimension: scalar | string | vector | matrix
i,j
2-dimensional indices.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | string | vector | matrix
s1,s2,...,sn
N-dimensional indices.
Type: double | integer | char | string | logical | struct | cell
Dimension: matrix

Outputs

ind
Linear index.

Examples

Simple sub2ind example.

sub2ind([2,4;5,2],2,5)
ans = 10

sub2ind example with a 3-dimensional matrix of size 2x5x3

mat3D = randn(2,5,3);
	  sub2ind(size(mat3D),2,3,1)
ans = 6