shiftdim
Shifts the dimensions of an array, removing or adding leading singleton dimensions.
Category: Language Fundamentals > Matrices and Arrays
Syntax
B = shiftdim(A)
Removes leading singleton dimensions.
B = shiftdim(A, n)
Shifts dimensions left by n (cyclically); a negative n adds -n leading singletons.
[B, ns] = shiftdim(A)
Also returns the number of dimensions removed.
Description
A common case is an array of size 1-by-1-by-k, produced by slicing a 3-D array; shiftdim turns it into a plain k-by-1 vector. With a positive n the first n dimensions move to the end, so an array of size [2 3 4] shifted by 1 has size [3 4 2]. This is the same as a permute by a rotated order.
Input arguments
A- Array of any type.
n- Number of positions to shift; integer.
Output arguments
B- Array with the same elements and shifted dimensions.
ns- Number of leading singleton dimensions that were removed.
Example
x = ones(1, 1, 3);
size(shiftdim(x))
size(shiftdim(ones(2, 3, 4), 1))
size(shiftdim(ones(2, 3), -1))
Output
ans = 1×2
3 1
ans = 1×3
3 4 2
ans = 1×3
1 2 3