File Exchange

image thumbnail

Permute a Matrix

version (1.55 KB) by Andrey Popov
From given order of rearrangement constructs the permutation matrix P and computes B = P'*A*P

1 Download

Updated 29 Oct 2009

View Version History

View License

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]

Cite As

Andrey Popov (2021). Permute a Matrix (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)


Jos (10584)

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

Jos (10584)

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.

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!