Main Content

shiftdim

Shift array dimensions

Description

example

B= shiftdim(A,n)shifts the dimensions of an arrayAbynpositions.shiftdimshifts the dimensions to the left whenn是一个积极的integer and to the right whennis a negative integer. For example, ifAis a 2-by-3-by-4 array, thenshiftdim(A,2)returns a 4-by-2-by-3 array.

B= shiftdim(A)returns an array with the same elements asAbut with leading dimensions of length 1 removed.

example

[B,m] = shiftdim(A)长度也回报维度的数量1that were removed.

Examples

collapse all

Create a 4-by-2-by-3-by-5 array. Shift the dimensions 2 positions to the left, wrapping the first 2 lengths to the last 2 dimensions.

A = rand(4,2,3,5); B = shiftdim(A,2); size(B)
ans =1×43 5 4 2

Shift the dimensions 2 positions to the right, resulting in 2 leading dimensions of length 1. Shifting to the right does not wrap the dimension lengths.

C = shiftdim(A,-2); size(C)
ans =1×61 1 4 2 3 5

Shift the dimensions of an array.

Compute a 5-D arrayA, and remove the leading dimensions of length 1. Theshiftdimfunction shifts 2 dimensions and returns the 3-D arrayB.

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

Shift the dimensions ofBtwice to the left.

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

Shift the dimensions ofConce to the right.

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

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Number of dimension positions to shift, specified as an integer. Whennis positive,shiftdimshifts the dimensions to the left, wrapping the leading dimensions to the end. Whennis negative,shiftdimshifts the dimensions to the right, padding additional leading dimensions with length 1.

Output Arguments

collapse all

Output array, specified as a vector, matrix, or multidimensional array.

Number of dimensions removed, specified as a non-negative integer.shiftdimremoves only leading dimensions of length 1.

Extended Capabilities

Version History

Introduced before R2006a