This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Shift dimensions


B = shiftdim(X,n)
[B,nshifts] = shiftdim(X)


B = shiftdim(X,n) shifts the dimensions of X by n. When n is positive, shiftdim shifts the dimensions to the left and wraps the n leading dimensions to the end. When n is negative, shiftdim shifts the dimensions to the right and pads with singletons.

[B,nshifts] = shiftdim(X) returns the array B with the same number of elements as X but with any leading singleton dimensions removed. A singleton dimension is any dimension for which size(A,dim) = 1. nshifts is the number of dimensions that are removed.

If X is a scalar, shiftdim has no effect.


collapse all

Shift the dimensions of an array.

Compute a 5-D array A, and remove the leading dimensions of length 1. The shiftdim function shifts 2 dimensions and returns the 3-D array B.

A = rand(1,1,3,2,4);
[B,nshifts] = shiftdim(A);
nshifts = 2
ans = 1×3

     3     2     4

Shift the dimensions of B twice to the left.

C = shiftdim(B,2);
ans = 1×3

     4     3     2

Shift the dimensions of C once to the right.

D = shiftdim(C,-1); 
ans = 1×4

     1     4     3     2

Extended Capabilities

Introduced before R2006a