slicedim

slicedim(A, d, i)

Return all the data of A where the index for dimension d equals i. Equivalent to A[:,:,...,i,:,:,...] where i is in position d.

Examples

  1. Slice a specific dimension of an array:

    julia> A = [1 2 3; 4 5 6; 7 8 9];
    julia> slicedim(A, 2, 2)
    3-element Array{Int64,1}:
    2
    5
    8

    This example slices the second dimension (d = 2) of the array A and returns the elements where the index is equal to i = 2.

  2. Slice a higher-dimensional array:

    julia> B = rand(2, 3, 4);
    julia> slicedim(B, 1, 2)
    3×4 Array{Float64,2}:
    0.697879  0.390278  0.775196  0.607335
    0.474109  0.682906  0.999054  0.136312
    0.867209  0.923193  0.849379  0.942011

    In this example, we slice the first dimension (d = 1) of the 3D array B and return the corresponding elements where the index is i = 2.

  3. Slice a specific dimension of a matrix:
    julia> C = [1 2 3; 4 5 6; 7 8 9];
    julia> slicedim(C, 2, 3)
    3-element Array{Int64,1}:
    3
    6
    9

    Here, we slice the second dimension (d = 2) of the matrix C and return the elements where the index is equal to i = 3.

Common mistake example:

julia> D = [10 20; 30 40];
julia> slicedim(D, 3, 1)
ERROR: DimensionError: dimension out of range

In this case, the provided dimension (d) is out of range for the array D. It's crucial to ensure that the dimension specified is valid for the given array. Always check the dimensions and their ranges before using the slicedim function.

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: