ind2sub(a, index)

ind2sub(a, index) -> subscripts

Returns a tuple of subscripts into array a corresponding to the linear index index

Examples

  1. 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 index 17 in an array with dimensions (3, 4, 2).

  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 matrix A.

  3. 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 variables i and j 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.

*Required Field
Details

Checking you are not a robot: