circshift
Shifting the array in a circle.
| Library |
|
Syntax
Function call
-
Y = circshift(A,K)— cyclically shifts array elementsAonKpositions. IfK— an integer, thencircshiftshifts elements along the first dimension of the arrayA, the size of which is not equal to1. IfKis a vector of integers, then each elementKspecifies the amount of the shift in the corresponding dimension of the arrayA.
Arguments
Input arguments
# A — input array
+
vector | the matrix
Details
An input array specified as a vector or matrix.
| Data types |
|
| Support for complex numbers |
Yes |
# K is the magnitude of the shift
+
an integer scalar | a vector of integers
Details
The value of the shift, specified as an integer scalar or a vector of integers.
-
If you specify
Kas an integer and not specifieddimThencircshiftwill perform a shift on the first dimension, the size of which is not equal to1. Positive valueKshifts it to the end of the measurement, and the negative one to the beginning. -
If you specify
Kas a vector of integers, then -th elementKsets the amount of shift for -th dimension inA. If -th element inKIf positive, then the valuesAmoving towards the end - th dimension. If -th element inAIf it is negative, then the values are shifted to the beginning.
If the value of the shift is greater than the length of the corresponding measurement in A, then the shift is cyclically transferred to the beginning of this measurement. For example, shifting a three-element vector by +3 positions returns its elements to their original positions.
# dim — the measurement for which the operation is performed
+
a positive integer scalar
Details
The dimension that the operation is performed on, specified as a positive integer scalar. If no value is specified, the first dimension is used by default, the size of which is not equal to 1. If the argument is dim specified, then K must be an integer scalar. In general, to exchange lines, specify dim = 1, for exchanging columns — dim = 2 etc.
Examples
Shifting the elements of a vector column
Details
Let’s create a numeric column vector.
A = [1:10...]
10-element Vector{Int64}:
1
2
3
4
5
6
7
8
9
10
We use circshift to shift the elements by three positions.
import EngeeDSP.Functions: circshift
Y = circshift(A,3)
10-element Vector{Int64}:
8
9
10
1
2
3
4
5
6
7
Result Y it has the same elements as A but they are arranged in a different order.
Shifting characters in an array
Details
Create an array of characters and use the function circshift to shift characters to 3 positions. The characters are in a different order in Y.
import EngeeDSP.Functions: circshift
A = "racecar"
Y = circshift(A,3)
"carrace"
Shifting matrix elements
Details
Creating a numeric array with a cluster of units in the upper-left corner.
A = [1 1 0 0; 1 1 0 0; 0 0 0 0; 0 0 0 0]
4×4 Matrix{Int64}:
1 1 0 0
1 1 0 0
0 0 0 0
0 0 0 0
We use circshift to shift each column A one position to the right.
import EngeeDSP.Functions: circshift
Y = circshift(A,1,2)
4×4 Matrix{Int64}:
0 1 1 0
0 1 1 0
0 0 0 0
0 0 0 0
Shift the elements of the matrix A one position in each dimension. The group of units is now in the center of the matrix.
Y = circshift(A,[1, 1])
4×4 Matrix{Int64}:
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
To return the cluster to its original position, use the function circshift for Y with negative shift values. The matrix X is equivalent to the matrix A.
X = circshift(Y,[-1, -1])
4×4 Matrix{Int64}:
1 1 0 0
1 1 0 0
0 0 0 0
0 0 0 0