Convert linear indices to subscripts
[
returns I1,I2,...,In
] = ind2sub(sz
,ind
)n
arrays I1,I2,...,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.
To get the linear indices of matrix elements that satisfy a specific condition for
matrix A
, you can use the find
function with one
output argument. To get the subscript indices, use the find
function
with two output arguments. For example, [row,col] =
ind2sub(size(A),find(A>5))
gives the same result as [row,col] =
find(A>5)
.
For an array A
, if [I1,…,In] = ind2sub(size(A),ind)
,
then A(I1(k),…,In(k)) = A(ind(k))
for all k
.