circshift — Rotate array elements circularly along one or more dimensions in MATLAB and RunMat.
circshift(A, K) circularly rotates elements of A. Positive shifts move toward higher indices with wraparound, and shifts can be supplied per-dimension or with explicit dimension arguments, consistent with MATLAB and RunMat.
Syntax
B = circshift(A, K)
B = circshift(A, K, dim)Inputs
A- Type
Any- Required
- Yes
- Description
- Input array.
K- Type
Any- Required
- Yes
- Description
- Shift scalar or vector.
dim- Type
Any- Required
- Yes
- Description
- Dimension index scalar or dimension vector.
Returns
B- Type
Any- Description
- Array with elements circularly shifted.
Errors
RunMat:circshift:TooManyInputs- When
- More than three input arguments are supplied.
- Message
circshift: too many input arguments
RunMat:circshift:InvalidShift- When
- Shift argument is not a finite real integer scalar/vector.
- Message
circshift: invalid shift argument
RunMat:circshift:InvalidDimensions- When
- Dimension argument is invalid or not compatible with shift argument.
- Message
circshift: invalid dimension argument
Behavior and placement
circshift(A, k) shifts by k positions along the first non-singleton dimension. Negative values shift in the opposite direction.
circshift(A, shiftVec) accepts a numeric vector that supplies a shift per dimension. Missing dimensions default to zero. Extra dimensions are treated as size-one axes, so they have no effect unless an explicit reshape added those dimensions previously.
circshift(A, K, dims) lets you target specific dimensions. K and dims must have the same length; each entry in dims is 1-based.
Works for numeric tensors, logical masks, complex arrays, string arrays, character matrices, and structure arrays. Shifts along implicit singleton dimensions leave a two-dimensional character matrix unchanged.
Empty dimensions remain empty; shifting them is a no-op. Scalars are returned unchanged.
Shifts that are integer multiples of a dimension’s extent leave that axis unchanged. RunMat reduces each shift modulo the axis length, matching MATLAB.
Numeric reordering preserves native single, double, logical, complex, and all eight integer storage classes where supported. Structure arrays are reordered by their owning structural path; cell arrays are supported only by flipud.
Explicit gpuArray inputs remain with their exact owner when the provider can execute or restore the result; automatic device placement may restore to host. Controls are validated before provider effects.
GPU execution and fusion
The current resident route checks the owning provider's circshift hook before using it, then validates the result. Unsupported hooks use an exact gather, host reorder, and owner-provider upload when available. This is a structural fusion boundary, with no scalar broadcasting.
Examples
Shifting matrix rows downward by one location
A = [1 2 3; 4 5 6; 7 8 9];
B = circshift(A, 1)Expected output:
B =
7 8 9
1 2 3
4 5 6Shifting columns left by two positions
A = reshape(1:12, [3 4]);
C = circshift(A, [0 -2])Expected output:
C =
7 10 1 4
8 11 2 5
9 12 3 6Rotating a 3-D tensor along multiple dimensions
T = reshape(1:8, [2 2 2]);
U = circshift(T, [1 0 -1]);
U(:, :, 1)
U(:, :, 2)Expected output:
ans(:,:,1) =
6 8
5 7
ans(:,:,2) =
2 4
1 3Shifting along specific dimensions using the dims argument
A = reshape(1:12, [3 4]);
V = circshift(A, [2 -1], [1 2])Expected output:
V =
5 8 11 2
6 9 12 3
4 7 10 1Rotating a character matrix to cycle through rows and columns
C = ['r','u','n'; 'm','a','t'];
R = circshift(C, [0 1])Expected output:
R =
'nru'
'tma'Applying circshift to a gpuArray and keeping the result on the device
G = gpuArray(reshape(1:12, [3 4]));
H = circshift(G, [1 -2]);
isgpuarray(H)Expected output:
ans = logical 1Using circshift with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how circshift changes the result.
Run a small circshift example, explain the result, then change one input and compare the output.
FAQ
Do K and dims need the same length?⌄
Yes. When you supply the dims argument, the shift vector must have the same number of entries. Each shift is paired with the corresponding dimension.
Can I shift along dimensions that are currently size one?⌄
You can, and the result matches MATLAB: the axis length is one so the shift has no visible effect.
How are negative shifts handled?⌄
They rotate values toward lower indices. Internally RunMat reduces each shift modulo the axis length, so -1 on a length-4 dimension is equivalent to 3.
What happens with empty tensors?⌄
The result remains empty with identical shape metadata. circshift never introduces new elements.
Are logical and string arrays supported?⌄
Yes. Logical arrays stay logical, and string arrays preserve their individual strings while rotating their positions.
Can character arrays be shifted along the third dimension?⌄
An axis beyond a character matrix's stored rank is an implicit singleton, so such a shift leaves the matrix unchanged.
Does circshift fuse with other GPU kernels?⌄
No. The call is a structural fusion boundary. An owning provider may still execute the reorder on-device through its circshift hook.
How does circshift interact with complex numbers?⌄
Real and imaginary parts move together. Scalars are unaffected, and arrays retain their complex entries without modification.
What if I pass non-integer shifts?⌄
RunMat enforces MATLAB semantics: shifts must be finite integers. Fractional values raise an error.
Does the builtin allocate new GPU buffers?⌄
A nontrivial resident shift validates an independent result handle. A modulo-zero shift is an identity and may return the original handle without allocating.
What does circshift do in MATLAB?⌄
circshift(A, K) circularly shifts the elements of array A by K positions. Elements shifted off one end wrap around to the other. Negative shifts move elements in the opposite direction.
How do I shift rows and columns separately with circshift?⌄
Use circshift(A, [rowShift, colShift]) to shift rows and columns independently. For example, circshift(A, [1, -2]) shifts rows down by 1 and columns left by 2.
Does circshift support GPU arrays?⌄
Yes. Explicit GPU arrays are handled by their owning provider or an exact gather-and-restore path. The structural call does not join an elementwise fusion region.
Related Array functions
Shape
blkdiag · cat · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how circshift is executed, line by line, in Rust.
- View the source for circshift in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.