Main Content

shiftdata

Shift data to operate on specified dimension

Description

[y,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. perm is the permutation that the function uses.

Note

Use the shiftdata function in tandem with the unshiftdata function, 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.

example

Examples

collapse all

Shift a 3-by-3 magic square, permuting the second dimension to the first column. Shift the matrix back to its original shape.

Create a 3-by-3 magic square.

x = magic(3)
x = 3×3

     8     1     6
     3     5     7
     4     9     2

Shift the matrix to work along the second dimension. Return the permutation vector, the number of shifts, and the shifted matrix.

[x,perm,nshifts] = shiftdata(x,2)
x = 3×3

     8     3     4
     1     5     9
     6     7     2

perm = 1×2

     2     1

nshifts =

     []

Restore the matrix back to its original shape.

y = unshiftdata(x,perm,nshifts)
y = 3×3

     8     1     6
     3     5     7
     4     9     2

Define the data to shift as a row vector.

x = 1:5
x = 1×5

     1     2     3     4     5

Define dim as empty to shift the first nonsingleton dimension of the data to the first column. shiftdata returns the data as a column vector, the permutation vector, and the number of shifts.

dim = [];
[x,perm,nshifts] = shiftdata(x,dim)
x = 5×1

     1
     2
     3
     4
     5

perm =

     []
nshifts = 
1

Restore the shifted data to its original shape.

y = unshiftdata(x,perm,nshifts)
y = 1×5

     1     2     3     4     5

Input Arguments

collapse all

Data, specified as a vector or matrix.

Data Types: single | double

Dimension to operate along, specified as a positive integer or []. If dim is [], then the function shifts the first nonsingleton dimension to the first column and returns the number of shifts in nshifts.

Data Types: single | double

Output Arguments

collapse all

Shifted data, returned as a vector or matrix.

Permutation used to shift the data, returned as a vector.

Number of shifts, returned as a scalar.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2012b

expand all