Engee documentation

circshift

Shifting the array in a circle.

Library

EngeeDSP

Syntax

Function call

  • Y = circshift(A,K) — cyclically shifts array elements A on K positions. If K — an integer, then circshift shifts elements along the first dimension of the array A, the size of which is not equal to 1. If K is a vector of integers, then each element K specifies the amount of the shift in the corresponding dimension of the array A.

  • Y = circshift(A,K,dim) — cyclically shifts the values of the array A on K measurement positions dim. Input data K and dim they must be scalars.

Arguments

Input arguments

# A — input array

+ vector | the matrix

Details

An input array specified as a vector or matrix.

Data types

Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Bool, Char, String

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 K as an integer and not specified dim Then circshift will perform a shift on the first dimension, the size of which is not equal to 1. Positive value K shifts it to the end of the measurement, and the negative one to the beginning.

  • If you specify K as a vector of integers, then -th element K sets the amount of shift for -th dimension in A. If -th element in K If positive, then the values A moving towards the end - th dimension. If -th element in A If 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.

Output arguments

# Y — output array

+ vector | the matrix

Details

The output array returned as a vector or matrix.

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