Shift array circularly
Y = circshift( circularly
shifts the elements in array
K is an integer, then
along the first dimension of
A whose size does
not equal 1. If
K is a vector of integers, then
each element of
K indicates the shift amount in
the corresponding dimension of
The default behavior of
a scalar changed in R2016b. To preserve the behavior of R2016a and
previous releases, use
circshift(A,K,1). This syntax
specifies 1 as the dimension to operate along.
Create a numeric column vector.
A = (1:10)'
A = 10×1 1 2 3 4 5 6 7 8 9 10
circshift to shift the elements by three positions.
Y = circshift(A,3)
Y = 10×1 8 9 10 1 2 3 4 5 6 7
Y, has the same elements as
A but they are in a different order.
Create an array of characters and use
circshift to shift the characters by 3 positions. The characters are in a different order in
A = 'racecar'; Y = circshift(A,3)
Y = 'carrace'
Create a numeric array with a cluster of ones in the top left.
A = [1 1 0 0; 1 1 0 0; 0 0 0 0; 0 0 0 0]
A = 4×4 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0
circshift to shift each row of
A one position to the right.
Y = circshift(A,1,2)
Y = 4×4 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0
Shift the elements of
A by one position in each dimension. The cluster of ones is now in the center of the matrix.
Y = circshift(A,[1 1])
Y = 4×4 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0
To move the cluster back to its original position, use
Y with negative shift values. The matrix
X is equivalent to
X = circshift(Y,[-1 -1])
X = 4×4 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0
A— Input array
Input array, specified as a vector, matrix, or multidimensional array.
Complex Number Support: Yes
K— Shift amount
Shift amount, specified as an integer scalar or vector of integers.
If you specify
K as an integer
and do not specify
along the first dimension whose size does not equal 1. Positive
toward the end of the dimension and negative
toward the beginning.
If you specify
K as a vector of
integers, then the Nth element in
the shift amount for the Nth dimension in
If the Nth element in
K is positive,
then the values of
A shift toward the end of the Nth
dimension. If the Nth element is negative, then
the values shift toward the beginning.
If the shift amount is greater than the length of the corresponding
A, then the shift circularly wraps
to the beginning of that dimension. For example, shifting a 3-element
vector by +3 positions brings its elements back to their original
dim— Dimension to operate along
Dimension to operate along, specified as a positive integer
scalar. If no value is specified, the default is the first dimension
whose size does not equal 1. If you specify
K must be an integer scalar. In general, specify
= 1 to exchange rows,
dim = 2 to exchange
columns, and so on.
Usage notes and limitations:
Code generation does not support tables and cells for the first input argument.
backgroundPoolor accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).