circshift
Shifts array elements circularly.
Category: Language Fundamentals > Matrices and Arrays
Syntax
B = circshift(A, k)
Shifts along the first non-singleton dimension by k positions.
B = circshift(A, k, dim)
Shifts along dimension dim.
Description
Elements pushed off one end re-enter at the other, so nothing is lost and the size does not change. A shift larger than the dimension wraps around, so shifting a length-5 vector by 7 is the same as shifting by 2.
For a matrix, a shift along dimension 1 moves rows and along dimension 2 moves columns.
Input arguments
A- Array of any type.
k- Integer shift. Positive moves elements toward higher indices, negative toward lower.
dim- Dimension to shift along.
Output arguments
B- Array of the same size as
A.
Example
circshift(1:5, 2)
circshift(1:5, -1)
circshift([1 2 3; 4 5 6], 1, 2)
Output
ans = 1×5
4 5 1 2 3
ans = 1×5
2 3 4 5 1
ans = 2×3
3 1 2
6 4 5