MATLAB eig有时会返回反转的符号

问题描述 投票:5回答:1

我正在尝试编写一个程序来获取任何大小的矩阵A,并且SVD将其分解:

A = U * S * V'

其中A是用户输入的矩阵,U是由A * A'的特征向量组成的正交矩阵,S是奇异值的对角矩阵,VA' * A的特征向量的正交矩阵。

问题是:MATLAB函数eig有时会返回错误的特征向量。

这是我的代码:

function [U,S,V]=badsvd(A)
W=A*A';
[U,S]=eig(W);
max=0;
for i=1:size(W,1) %%sort
    for j=i:size(W,1)
        if(S(j,j)>max)
            max=S(j,j);
            temp_index=j;
        end
    end
    max=0;
    temp=S(temp_index,temp_index);
    S(temp_index,temp_index)=S(i,i);
    S(i,i)=temp;
    temp=U(:,temp_index);
    U(:,temp_index)=U(:,i);
    U(:,i)=temp;
end
W=A'*A;
[V,s]=eig(W);
max=0;
for i=1:size(W,1) %%sort
    for j=i:size(W,1)
        if(s(j,j)>max)
            max=s(j,j);
            temp_index=j;
        end
    end
    max=0;
    temp=s(temp_index,temp_index);
    s(temp_index,temp_index)=s(i,i);
    s(i,i)=temp;
    temp=V(:,temp_index);
    V(:,temp_index)=V(:,i);
    V(:,i)=temp;
end
s=sqrt(s);
end

我的代码返回正确的s矩阵,并且“几乎”正确的UV矩阵。但有些列乘以-1。很明显,如果t是一个特征向量,那么-t也是一个特征向量,但是反转的符号(对于某些列而不是全部)我得不到A = U * S * V'

有没有什么办法解决这一问题?

示例:对于矩阵A=[1,2;3,4],我的函数返回:

U=[0.4046,-0.9145;0.9145,0.4046]

并且内置的MATLAB svd函数返回:

u=[-0.4046,-0.9145;-0.9145,0.4046]
matlab matrix linear-algebra svd eigenvector
1个回答
12
投票

请注意,特征向量不是唯一的。乘以任何常数,包括-1(只是改变符号),给出另一个有效的特征向量。鉴于特征向量的definition,这很清楚:

A·v = λ·v

MATLAB chooses将特征向量标准化为1.0的范数,符号是任意的:

对于eig(A),特征向量被缩放,使得每个的范数为1.0。对于eig(A,B)eig(A,'nobalance')eig(A,B,flag),特征向量未归一化

如你所知,SVD和特征分解是related。下面是一些测试这个事实的代码。请注意,svdeig以不同的顺序返回结果(一个从高到低排序,另一个在反向排序):

% some random matrix
A = rand(5);

% singular value decomposition
[U,S,V] = svd(A);

% eigenvectors of A'*A are the same as the right-singular vectors
[V2,D2] = eig(A'*A);
[D2,ord] = sort(diag(D2), 'descend');
S2 = diag(sqrt(D2));
V2 = V2(:,ord);

% eigenvectors of A*A' are the same as the left-singular vectors
[U2,D2] = eig(A*A');
[D2,ord] = sort(diag(D2), 'descend');
S3 = diag(sqrt(D2));
U2 = U2(:,ord);

% check results
A
U*S*V'
U2*S2*V2'

我得到非常相似的结果(忽略小的浮点错误):

>> norm(A - U*S*V')
ans =
   7.5771e-16
>> norm(A - U2*S2*V2')
ans =
   3.2841e-14

编辑:

为了获得一致的结果,人们通常采用要求每个特征向量中的第一个元素具有特定符号的约定。这样,如果你得到一个不符合这个规则的特征向量,你将它乘以-1来翻转符号......

© www.soinside.com 2019 - 2024. All rights reserved.