circshift

circshift(A,shifts)

Circularly shift the data in an array. The second argument is a vector giving the amount to shift in each dimension.

Examples

  1. Circularly shift a 1-dimensional array:

    julia> arr = [1, 2, 3, 4, 5];
    julia> circshift(arr, 2)
    5-element Array{Int64,1}:
    4
    5
    1
    2
    3

    This example circularly shifts the elements of the array arr by 2 positions to the right.

  2. Circularly shift a 2-dimensional array:

    julia> matrix = [1 2 3; 4 5 6; 7 8 9];
    julia> circshift(matrix, (0, 1))
    3×3 Array{Int64,2}:
    3  1  2
    6  4  5
    9  7  8

    It circularly shifts the elements of the 2-dimensional matrix matrix by 1 position to the right along the second dimension.

  3. Perform circular shift along multiple dimensions:

    julia> tensor = reshape(collect(1:27), 3, 3, 3);
    julia> circshift(tensor, (1, 0, -1))
    3×3×3 Array{Int64,3}:
    [:, :, 1] =
    3   6   9
    2   5   8
    1   4   7
    
    [:, :, 2] =
    12  15  18
    11  14  17
    10  13  16
    
    [:, :, 3] =
    21  24  27
    20  23  26
    19  22  25

    This example showcases circular shifting of a 3-dimensional tensor tensor along all dimensions.

Common mistake example:

julia> arr = [1, 2, 3, 4, 5];
julia> circshift(arr, 7)
ERROR: BoundsError: attempt to access 5-element Array{Int64,1} at index [7]

In this example, the shift amount provided is greater than the size of the array. Ensure that the shift amount for each dimension is within the valid range to avoid such errors.

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: