sub2ind, ind2sub
sub2ind
ind = sub2ind(sz,I1,...,In)returns the linear indices ind corresponding to the multidimensional subscripts I1, …, In for an array of size sz- sz is a vector with n (or more) elements giving the size of each array dimension
- Each returned value is a single linear index
- I1, …, In must be vectors of equal length
Example
The mapping from subscripts (indexing by position) to linear indices for a 3-by-3 matrix is illustrated below.

Specify row and column subscripts in a 3-by-3 matrix and convert them to linear indices:
>> row = [1 2 3 1];
>> col = [2 2 2 3];
>> sz = [3 3];
>> ind = sub2ind(sz,row,col)
ind =
4 5 6 7ind2sub
[I1,I2,...,In] = ind2sub(sz,ind)returns n arrays I1, …, In containing the multidimensional subscripts equivalent to the linear indices ind for an array of size sz- sz is a vector with n elements giving the size of each array dimension
- ind must be a vector or scalar of single linear indices