Skip to content

ind2sub

Convert a linear index to N-dimensional subscripts.

[I1, I2, ...] = ind2sub(siz, IND)

Inverse of sub2ind. Given a size vector siz and one or more linear indices IND, returns matched arrays of subscripts — one output per dimension. Number of outputs is determined by how the call is destructured ([i, j] = … requests two; [i, j, k] = … requests three).

When IND is a vector or matrix, each output has the same shape as IND.

[i, j] = ind2sub([3 4], 8) % i=2, j=3
[i, j] = ind2sub([3 4], [1 5 8]) % i=[1 2 2], j=[1 2 3]
  • sub2ind — Convert N-dimensional subscripts to a single linear index.
  • size — Sizes along all dimensions, or along one specific dimension.