# shiftdata

Shift data to operate on specified dimension

## Syntax

```[x,perm,nshifts] = shiftdata(x,dim) ```

## Description

`[x,perm,nshifts] = shiftdata(x,dim)` shifts data `x` to permute dimension `dim` to the first column using the same permutation as the built-in `filter` function. The vector `perm` returns the permutation vector that is used.

If `dim` is missing or empty, then the first non-singleton dimension is shifted to the first column, and the number of shifts is returned in `nshifts`.

`shiftdata` is meant to be used in tandem with `unshiftdata`, which shifts the data back to its original shape. These functions are useful for creating functions that work along a certain dimension, like `filter`, `goertzel`, `sgolayfilt`, and `sosfilt`.

## Examples

collapse all

1. Create a 3-x-3 magic square:

`x = fi(magic(3))`
```x = 8 1 6 3 5 7 4 9 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11```
2. Shift the matrix `x` to work along the second dimension:

`[x,perm,nshifts] = shiftdata(x,2)`
```x = 8 3 4 1 5 9 6 7 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11 perm = 2 1 nshifts = []```

The permutation vector, `perm`, and the number of shifts, `nshifts`, are returned along with the shifted matrix, `x`.

3. Shift the matrix back to its original shape:

`y = unshiftdata(x,perm,nshifts)`
```y = 8 1 6 3 5 7 4 9 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11```
1. Define `x` as a row vector:

`x = 1:5`
```x = 1 2 3 4 5```
2. Define `dim` as empty to shift the first non-singleton dimension of `x` to the first column:

`[x,perm,nshifts] = shiftdata(x,[])`
```x = 1 2 3 4 5 perm = [] nshifts = 1```

`x` is returned as a column vector, along with `perm`, the permutation vector, and `nshifts`, the number of shifts.

3. Using `unshiftdata`, restore `x` to its original shape:

`y = unshiftdata(x,perm,nshifts)`
```y = 1 2 3 4 5```

## Version History

Introduced in R2008a