File Exchange

## Permute a Matrix

version 1.1.0.0 (1.55 KB) by Andrey Popov

### Andrey Popov (view profile)

From given order of rearrangement constructs the permutation matrix P and computes B = P'*A*P

Updated 29 Oct 2009

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, ...

Example
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]

### Cite As

Andrey Popov (2019). Permute a Matrix (https://www.mathworks.com/matlabcentral/fileexchange/25148-permute-a-matrix), MATLAB Central File Exchange. Retrieved .

Elmar

Jos (10584)

### Jos (10584) (view profile)

I was a little sloppy .. I meant the second output ...

Jos (10584)

### Jos (10584) (view profile)

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.