PERMM Permutates a matrix A by given indexes by computing B = P'*A*P
B = permm(A,indx)
[B,P] = permm(A,indx)
The function applies simultaneous column and row permutation, that is by
given indexes indx = [k j m ...] the function rearranges the elements of A
so that B11=Akk, B12=Akj, ... B21 = Ajk, B22=Ajj, ...
A = [11 12 13 % initial matrix
21 22 23
31 32 33]
idp = [2 3 1] % order of rearrangement
B = permm(A, idp); % rearranged matrix
% B = [22 23 21
% 32 33 31
% 12 13 11]
Andrey Popov (2020). Permute a Matrix (https://www.mathworks.com/matlabcentral/fileexchange/25148-permute-a-matrix), MATLAB Central File Exchange. Retrieved .
I was a little sloppy .. I meant the second output ...
This function does not work (and rightfully so!) when 1) the input is not square or when idp does not contain a permutation of all N numbers between 1 and N (for instance [1 3 3]). However, such likely inputs lead to unfriendly error messages, so I suggest you add some error checks in your code.
Furthermore, P can be defined more easily as
P = eye(size(A)) ;
P = P(idp,:) ;
Finally, the help section is quite nice, but lacks a description of the third output. And, btw, is the copyright really needed given the BSD license. So, room for improvement.
Implements some checks of the input variables and an easier construction of the permutation matrix.