# Documentation

### This is machine translation

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

To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

# shiftdim

Shift dimensions

## Syntax

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

## Description

`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.

## Examples

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```
```nshifts = 2 ```
`size(B)`
```ans = 3 2 4 ```

Shift the dimensions of `B` twice to the left.

```C = shiftdim(B,2); size(C)```
```ans = 4 3 2 ```

Shift the dimensions of `C` once to the right.

```D = shiftdim(C,-1); size(D)```
```ans = 1 4 3 2 ```