This is a somewhat silly question but I can't seem to figure out how permute works in matlab. Take the documentation example:
A = [1 2; 3 4]; permute(A,[2 1])
ans =
1 3
2 4
What is going on? How does this tell matlab that the 3 and 2 need to be swapped?
Description. sz = size( A ) returns a row vector whose elements are the lengths of the corresponding dimensions of A . For example, if A is a 3-by-4 matrix, then size(A) returns the vector [3 4] .
B = shiftdim( A , n ) shifts the dimensions of an array A by n positions. shiftdim shifts the dimensions to the left when n is a positive integer and to the right when n is a negative integer.
Wow, this is one of the hardest functions to figure out among all the different SDKs I have used up to now. Therefore, I used the F*ck word many times during " my journey of understanding the permute function" . Here are some examples to prevent you from suffering a similar excruciating pain:
First, let's remember the dimensions' names of matrix in matlab: A = zeros(4,5,7)
, matrix A has 4 rows, 5 columns and 7 pages. And if you don't specify a dimension, its default count is set to 1. ( i.e. B=zeros(10,3)
has 10 rows, 3 columns and 1 page, this order is important!)
order
argument passed to permute
swap these dimensions in the matrix and produce an awkward combination of arrays, I think permute
is a misnomer for this effect.
Now let's move to the examples, Finally:
% A has 4 rows, 2 columns and 1 page
A =[ 5 6
8 2
2 2
1 3];
% (numbers in the order argument of permute function indicates dimensions,
% 3 = page , 2 = column and 1 = row dimensions):
B = permute(A,[3,2,1]); % [3,2,1] means [ page,column,row]
C = permute(A,[3,1,2]); % [3,1,2] means [ page,row,column]
D = permute(A,[1,3,2]); % [1,3,2] means [ row,page,column]
E = permute(A,[2,3,1]); % [2,3,1] means [ column,page,row]
F = permute(A,[2,1,3]); % [2,1,3] means [ column,row,page]
G = permute(A,[1,2,3]); % [1,2,3] means [ row,column,page]
EXPLANATIONS:
B = permute(A,[3,2,1]);
1x2x4 ( page(3) dimension of A = 1, column(2) dimension of A = 2, row(1) dimension of A = 4; 1 is row dimension, 2 is column dimension and 4 is page dimension for the generated B. Keep reading here until you understand) So, there will be 4 1x2 (1x2x4) row matrixes. As in:
ans(:,:,1) =
5 6
ans(:,:,2) =
8 2
ans(:,:,3) =
2 2
ans(:,:,4) =
1 3
*
C = permute(A,[3,1,2]);
1x4x2 ( page(3) dimension of A = 1, row(1) dimension of A = 4, column(2) dimension of A = 2; 1 is row dimension, 4 is column dimension and 2 is page dimension for the generated C) So, there will be 2 1x4 (1x4x2) row matrixes. As in:
ans(:,:,1) =
5 8 2 1
ans(:,:,2) =
6 2 2 3
*
D = permute(A,[1,3,2]);
4x1x2 ( row(1) dimension of A = 4, page(3) dimension of A = 1, column(2) dimension of A = 2; 4 is row dimension, 1 is column dimension and 2 is page dimension for the generated D) So, there will be 2 4x1 (4x1x2) column matrixes. As in:
ans(:,:,1) =
5
8
2
1
ans(:,:,2) =
6
2
2
3
*
E = permute(A,[2,3,1]);
2x1x4 ( column(2) dimension of A = 2, page(3) dimension of A = 1, row(1) dimension of A = 4; 2 is row dimension, 1 is column dimension and 4 is page dimension for the generated E) So, there will be 4 2x1 (2x1x4) column matrixes. As in:
ans(:,:,1) =
5
6
ans(:,:,2) =
8
2
ans(:,:,3) =
2
2
ans(:,:,4) =
1
3
*
F = permute(A,[2,1,3]); % this is transpose and same as [2,1]
2x4x1 ( column(2) dimension of A = 2, row(1) dimension of A = 4, page(3) dimension of A = 1; 2 is row dimension, 4 is column dimension and 1 is page dimension for the generated F) So, there will be 1 2x4 (2x4x1) matrix. As in:
ans =
5 8 2 1
6 2 2 3
*
G = permute(A,[1,2,3]); % this makes no difference, using to show the reasoning
4x2x1 ( row(1) dimension of A = 4, column(2) dimension of A = 2, page(3) dimension of A = 1; 4 is row dimension, 2 is column dimension and 1 is page dimension for the generated G) So, there will be 1 4x2 (4x2x1) matrix(itself!). As in:
ans =
5 6
8 2
2 2
1 3
Yes, this looks hard and it is indeed hard! To check if you understand thoroughly, try predicting a square Matrix's similar different permutations. Have fun, I mean have less pain :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With