Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: finding permutation matrices using eig()
Date: Wed, 20 Apr 2011 03:50:04 +0000 (UTC)
Organization: UCSD
Lines: 31
Message-ID: <ioll5c$a6k$1@fred.mathworks.com>
References: <hqrck0$pl8$1@fred.mathworks.com> <hqt3vs$s0f$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1303271404 10452 172.30.248.47 (20 Apr 2011 03:50:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 20 Apr 2011 03:50:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 368849
Xref: news.mathworks.com comp.soft-sys.matlab:722831

I have found Roger's code very useful for my application.  Thanks!
I'll just post a thing I had to fix.  Sometimes the permutation matrix has
values near 0.5 before thresholding as the comparisons done with min() come
out nearly a tie for me.   In the following code, I take all the close comparisons
are try flipping them...

SA = sort(VA); % Sort eigenvectors of A
SB = sort(VB); % Sort eigenvectors of B
MB = -flipud(SB); % Get sorted negatives of B eigenvectors
[t,ix] = min([sum(abs(SA-SB),1);sum(abs(SA-MB),1)],[],1); % Compare
[u,jx] = max([sum(abs(SA-SB),1);sum(abs(SA-MB),1)],[],1); % Compare
x=2*(ix==1)-1;
nVB = VB.*repmat(x,n,1); % Reverse signs accordingly
P = nVB*VA'; % Generate presumed permutation matrix
P = +(P>1-1e-12); % Turn all elements into 1's and 0's
uncert=find(u<1e-10);
if ~isempty(uncert) && (abs(det(P))~=1),
    e=[1;-1]; xm=x;
    for i=2:length(uncert),
        e=[ones(length(e),1) e; -1*ones(length(e),1) e];
    end
    for i=2:length(e), % already tried first one above
        xm(uncert)=x(uncert).*e(i,:);
        nVB = VB.*repmat(xm,n,1); % Reverse signs accordingly
        P = nVB*VA'; % Generate presumed permutation matrix
        P = +(P>1-1e-12); % Turn all elements into 1's and 0's
        if abs(det(P))==1,
            break;
        end
    end
end