circshift - Shift array circularly - MATLAB (original) (raw)
Syntax
Description
Y = circshift([A](#bt5nt1s-1-A),[K](#bt5nt1s-1-K))
circularly shifts the elements in array A
by K
positions. If K
is an integer, then circshift
shifts 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 A
.
Note
The default behavior of circshift(A,K)
where K
is 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.
Y = circshift([A](#bt5nt1s-1-A),[K](#bt5nt1s-1-K),[dim](#bt5nt1s-1-dim))
circularly shifts the values in array A
by K
positions along dimension dim
. Inputs K
and dim
must be scalars.
Examples
Create a numeric column vector.
A = 10×1
1
2
3
4
5
6
7
8
9
10
Use circshift
to shift the elements by three positions.
Y = 10×1
8
9
10
1
2
3
4
5
6
7
The result, 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 Y
.
A = 'racecar';
Y = circshift(A,3)
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
Use circshift
to shift each column of A
one position to the right.
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 = 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 circshift
on Y
with negative shift values. The matrix X
is equivalent to A
.
X = 4×4
1 1 0 0
1 1 0 0
0 0 0 0
0 0 0 0
Input Arguments
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 specify
K
as an integer and do not specifydim
, thencircshift
shifts along the first dimension whose size does not equal 1. PositiveK
shifts toward the end of the dimension and negativeK
shifts toward the beginning. - If you specify
K
as a vector of integers, then the _N_th element inK
specifies the shift amount for the _N_th dimension inA
. If the _N_th element inK
is positive, then the values ofA
shift toward the end of the _N_th dimension. If the _N_th element is negative, then the values shift toward the beginning.
If the shift amount is greater than the length of the corresponding dimension in 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 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 specify dim
, then K
must be an integer scalar. In general, specify dim = 1
to exchange rows, dim = 2
to exchange columns, and so on.
Extended Capabilities
Usage notes and limitations:
- Code generation does not support tables and cells for the first input argument.
The circshift
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a