ind2sub(a, index)
ind2sub(a, index) -> subscripts
Returns a tuple of subscripts into array a
corresponding to the linear index index
Examples
-
Get subscripts from linear index:
julia> dims = (3, 4, 2); julia> index = 17; julia> ind2sub(dims, index) (2, 3, 1)
This example returns the subscripts
(2, 3, 1)
corresponding to the linear index17
in an array with dimensions(3, 4, 2)
. -
Retrieve subscripts for maximum element:
julia> A = [4 2 5; 1 3 6]; julia> index = indmax(A); julia> i, j = ind2sub(size(A), index); julia> (i, j) (2, 3)
Here,
ind2sub
is used to obtain the subscripts(2, 3)
for the maximum element in the matrixA
. - Multiple subscripts assignment:
julia> dims = (2, 2); julia> index = 3; julia> i, j = ind2sub(dims, index); julia> (i, j) (2, 1)
In this example,
ind2sub
is used to assign the subscripts(2, 1)
to variablesi
andj
simultaneously.
It is worth noting that the number of subscripts returned by ind2sub
corresponds to the number of dimensions specified in dims
.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.