kusano 7d535a
% PERMUTATION : Helpful notes on permutation matrices and vectors.
kusano 7d535a
%
kusano 7d535a
%
kusano 7d535a
% There are two ways to represent permutations in Matlab.  
kusano 7d535a
% 
kusano 7d535a
% A *permutation matrix* is a square matrix P that is zero except for exactly
kusano 7d535a
% one 1 in each row and each column.  A *permutation vector* is a 1 by n
kusano 7d535a
% or n by 1 vector p that contains each of the integers 1:n exactly once.
kusano 7d535a
% 
kusano 7d535a
% 
kusano 7d535a
% Let A be any n by n matrix, P be an n by n permutation matrix, and p be
kusano 7d535a
% a permutation vector of length n.  Then:
kusano 7d535a
% 
kusano 7d535a
%   P*A is a permutation of the rows of A.
kusano 7d535a
%   A*P' is the same permutation of the columns of A.  Remember the transpose!
kusano 7d535a
% 
kusano 7d535a
%   A(p,:) is a permutation of the rows of A.
kusano 7d535a
%   A(:,p) is the same permutation of the columns of A.
kusano 7d535a
% 
kusano 7d535a
% 
kusano 7d535a
% Matrix P and column vector p represent the same permutation if and only if
kusano 7d535a
% any of the following three equivalent conditions holds:
kusano 7d535a
% 
kusano 7d535a
%   p = P*(1:n)'     or     P = sparse(1:n,p,1,n,n)     or     P = I(p,:),
kusano 7d535a
% 
kusano 7d535a
% where I = speye(n,n) is the identity matrix of the right size.
kusano 7d535a
% 
kusano 7d535a
% 
kusano 7d535a
% The inverse of a permutation matrix is its transpose:
kusano 7d535a
% 
kusano 7d535a
%   Pinv = inv(P) = P'
kusano 7d535a
% 
kusano 7d535a
% The inverse of a permutation vector can be computed by a one-liner:
kusano 7d535a
% 
kusano 7d535a
%   pinv(p) = 1:n;
kusano 7d535a
% 
kusano 7d535a
% (This works only if pinv doesn't already exist, or exists with the correct 
kusano 7d535a
% dimensions.  To be safe, say:  pinv=zeros(1,n); pinv(p)=1:n; )
kusano 7d535a
% 
kusano 7d535a
% 
kusano 7d535a
% Products of permutations go one way for matrices and the other way for
kusano 7d535a
% vectors.  If P1, P2, and P3 are permutation matrices and p1, p2, and p3
kusano 7d535a
% are the corresponding permutation vectors, then
kusano 7d535a
% 
kusano 7d535a
%   P1 = P2 * P3     if and only if     p1 = p3(p2).
kusano 7d535a
% 
kusano 7d535a
% 
kusano 7d535a
% Permutation vectors are a little more compact to store and efficient 
kusano 7d535a
% to apply than sparse permutation matrices (and much better than full
kusano 7d535a
% permutation matrices).  A sparse permutation matrix takes about twice
kusano 7d535a
% the memory of a permutation vector.  The Matlab function LU returns
kusano 7d535a
% a permutation matrix (sparse if the input was sparse); most other 
kusano 7d535a
% built-in Matlab functions return permutation vectors, including 
kusano 7d535a
% SYMRCM, SYMMMD, COLAMD, DMPERM, and ETREE.
kusano 7d535a
kusano 7d535a
% John Gilbert, 6 April 1995
kusano 7d535a
% Copyright (c) 1995 by Xerox Corporation.  All rights reserved.
kusano 7d535a
kusano 7d535a
help permutation