permute
Reorders the dimensions of an array.
Category: Language Fundamentals > Matrices and Arrays
Syntax
B = permute(A, order)
Rearranges the dimensions of A in the order given by order.
Description
permute(A, [2 1]) is the ordinary transpose. With three or more dimensions it moves whole axes: permute(A, [3 1 2]) makes the third dimension the first. Elements keep their values; only the indexing changes. It is often used to line up data so that a function operating along the first dimension can be applied to another axis.
ipermute applies the inverse reordering.
Input arguments
A- Array of any type.
order- Vector containing each dimension number exactly once, such as
[2 1]or[3 1 2].
Output arguments
B- Array with
size(B, k) == size(A, order(k)).
Example
A = reshape(1:6, 2, 3)
permute(A, [2 1])
size(permute(ones(2, 3, 4), [3 1 2]))
Output
A = 2×3
1 3 5
2 4 6
ans = 3×2
1 2
3 4
5 6
ans = 1×3
4 2 3