Main Content

circshift

Shift array circularly

Description

example

Y = circshift(A,K)circularly shifts the elements in arrayAbyKpositions. IfKis an integer, thencircshiftshifts along the first dimension ofAwhose size does not equal 1. IfKis a vector of integers, then each element ofKindicates the shift amount in the corresponding dimension ofA.

Note

The default behavior ofcircshift(A,K)whereKis a scalar changed in R2016b. To preserve the behavior of R2016a and previous releases, usecircshift(A,K,1). This syntax specifies 1 as the dimension to operate along.

example

Y = circshift(A,K,dim)circularly shifts the values in arrayAbyKpositions along dimensiondim. InputsKanddimmust be scalars.

Examples

collapse all

Create a numeric column vector.

A = (1:10)'
A =10×11 2 3 4 5 6 7 8 9 10

Usecircshiftto shift the elements by three positions.

Y = circshift(A,3)
Y =10×18 9 10 1 2 3 4 5 6 7

The result,Y, has the same elements asAbut they are in a different order.

Create an array of characters and usecircshiftto shift the characters by 3 positions. The characters are in a different order inY.

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×41 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0

Usecircshiftto shift each row ofAone position to the right.

Y = circshift(A,1,2)
Y =4×40 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0

Shift the elements ofAby one position in each dimension. The cluster of ones is now in the center of the matrix.

Y = circshift(A,[1 1])
Y =4×40 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0

To move the cluster back to its original position, usecircshiftonYwith negative shift values. The matrixX相当于A.

X = circshift(Y,[-1 -1])
X =4×41 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string|struct|table|cell
Complex Number Support:Yes

Shift amount, specified as an integer scalar or vector of integers.

  • If you specifyKas an integer and do not specifydim, thencircshiftshifts along the first dimension whose size does not equal 1. PositiveKshifts toward the end of the dimension and negativeKshifts toward the beginning.

  • If you specifyKas a vector of integers, then theNth element inKspecifies the shift amount for theNth dimension inA. If theNth element inKis positive, then the values ofAshift toward the end of theN维度。如果Nth element is negative, then the values shift toward the beginning.

如果shift amount is greater than the length of the corresponding dimension inA, 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 positions.

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 specifydim, thenKmust be an integer scalar. In general, specifydim = 1to exchange rows,dim = 2to exchange columns, and so on.

Extended Capabilities

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a