sub2ind, ind2sub
sub2ind
ind = sub2ind(sz,I1,...,In)returns the linear indices ind corresponding to multidimensional subscripts I1, …, In for a multidimensional array of size sz- Here sz is a vector with n (or greater than n) elements that specifies the size of each array dimension
- 返回的都是元素的 single linear index
- I1, …, In 为长度相同 vector
Example
The mapping from subscripts (indexing by position) to linear indices for a 3-by-3 matrix can be illustrated as in the following.

Specify the row and column subscripts in a 3-by-3 matrix. Convert the subscripts 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 equivalent multidimensional subscripts corresponding to the linear indices ind for a multidimensional array of size sz- Here sz is a vector with n elements that specifies the size of each array dimension
- ind 需为 single indices 组成的 vector 或 scalar