ind2sub

Returns subscript from the linear index ind.

Syntax

[i,j]=ind2sub(dim, ind)

[s1,s2,...,sn]=ind2sub(dim, ind)

Inputs

dim
Matrix dimensions.
Type: double | integer
Dimension: scalar | string | vector | matrix
ind
Linear index.
Type: double | integer
Dimension: scalar

Outputs

i,j
2-dimensional indices.
s1,s2,...,sn
N-dimensional indices.

Examples

Simple ind2sub example:

[i,j]=ind2sub([4,5], 7)
i = 3
j = 2

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

mat3D = randn(2,5,3);
	  [i,j,k]=ind2sub(size(mat3D), 10)
i = 2
j = 5
k = 1