shiftdimShift array dimensions
shiftdim(A, k)A should be an array and k should be a real integer scalar not equal to Inf nor NaN. It shifts the size vector of A k times.k is positive, the size vector is shifted to the left in a cyclic manner. (The first element is shifted to the last, the second to the first, etc.)k is negative, the size vector is shifted to the right and it packs k leading singleton dimensions to the shifted size vector.[M, d] = shiftdim(A)A. d is the number of leading singleton dimensions removed, and M is the array obtained after removing the leading dimensions.k. The array a has size [1, 2, 3]. Its size vector is shifted towards the left by 1 position in a cyclic manner. Thus, the output has the size [2, 3, 1].a=randi(10,1,2,3)
b=shiftdim(a,1)
disp('size before shifting')
disp(size(a))
disp('size after shifting')
disp(size(b))
a(:, :, 1) =
7.0000 7.0000
a(:, :, 2) =
1.0000 5.0000
a(:, :, 3) =
5.0000 7.0000
b =
7.0000 1.0000 5.0000
7.0000 5.0000 7.0000
size before shifting
1.0000 2.0000 3.0000
size after shifting
2.0000 3.0000 1.0000
k. Here k is negative (k == -2) and the dimensions are shifted to the right. It also packs two leading singleton dimensions with ones.a=randi(10,1,2,3);
% Size before shifting
size(a)
b=shiftdim(a,-2);
% Size after shifting to the right
size(b)
ans =
1.000 2.000 3.000
ans =
1.000 1.000 1.000 2.000 3.000
a has size [1, 1, 2, 3], where the first two dimensions are singleton. Then, shiftdim() removes the singleton dimensions to give M, which has the size [2, 3]. Also d is equal to 2 because two dimensions have been removed.a = rand(1,1,2,3)
[M,d] = shiftdim(a)
a(:, :, 1, 1) = 1e-1 ×
3.9131
a(:, :, 2, 1) = 1e-1 ×
9.5693
a(:, :, 1, 2) = 1e-1 ×
8.5735
a(:, :, 2, 2) = 1e-1 ×
6.5399
a(:, :, 1, 3) = 1e-1 ×
8.9036
a(:, :, 2, 3) = 1e-1 ×
3.4201
M = 1e-1 ×
3.9131 8.5735 8.9036
9.5693 6.5399 3.4201
d =
2.0000