permuteRearranging dimensions
b = permute(a, order)a is an array.a according to the order specified by order. The order(i)-th dimension of a is shifted to the i-th dimension in the new array b.ipermute.order should be a permutation of the vector 1:length(order) with length(order) >= ndims(a)a.a = [1 2 3;4 5 6];
permute(a,[2 1])
ans =
1.0000 4.0000
2.0000 5.0000
3.0000 6.0000
order has a length larger than ndims(a). Thus, 3rd, 4th, and 5th dimensions of a are considered as singleton, i.e., having lengths of 1. The 5th dimension of b is singleton, and therefore ndims(b) is 4.a = rand(3,4);
b = permute(a,[3 1 4 2 5]);
size(b)
ndims(b)
ans =
1.0000 3.0000 1.0000 4.0000 1.0000
ans =
4.0000
permute is the inverse operation of ipermute.a = rand(3,4);
b = ipermute(a,[3 1 4 2 5]);
c = permute(b,[3 1 4 2 5]);
% a and c have the same sizes
size(a),size(c)
% a and c are the same matrices
a - c
ans =
3.0000 4.0000
ans =
3.0000 4.0000 1.0000 1.0000 1.0000
ans =
0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000