我将如何重新排序对角矩阵的实部以及另一个矩阵中的相应特征向量?

How would I reorder the real parts of a diagonal matrix along with the corresponding eigenvectors in another matrix?

我正在处理教科书中的 MATLAB 问题,其中一个问题要求我在 MATLAB 中使用 eig 命令,计算矩阵 VD 使得 A = V * D * inv(V)。知道 V 的第一列对应于第一个特征值 D(1,1) 等等,我需要重新排序 D 的对角线条目,以便实部沿着对角线增加并重新排序V 的列因此 A = V * D * inv(V) 仍然成立。这是我到目前为止所写的内容:

r = RandStream('mt19937ar','Seed',1234);
A = r.randn(10,10)+1j*r.randn(10,10);
[V,D] = eig(A);

for tt = 1:9
    if (real(D(tt,tt)) > real(D(tt+1,tt+1)))
        temp = D(tt,tt);
        D(tt,tt) = D(tt+1,tt+1); 
        D(tt+1,tt+1) = temp;
        tempV = V(1,tt);
        V(1,tt) = V(1,tt+1);
        V(1,tt+1) = tempV;
        if (A == V*D*inv(V))
            break
        end
    end
end

我测试的时候,D的对角线元素和原来的顺序没有变化,我知道可能是我设置的条件的问题,但是我不确定具体是什么导致的什么都不做。我还认为我重新排序对角线元素和相应特征向量的方式可能存在问题。感谢您提供任何反馈或建议,提前致谢。

您的代码有多个问题:

  • sorting 需要两个 for 循环。
  • 您仅交换特征向量的第一个元素,对整列使用 V(:, tt)
  • V*D*inv(V) 永远不会完全等于 A(参见 this)。

要按实部对特征值进行排序,试试这个:

clc;
r = RandStream('mt19937ar','Seed',1234);
n = 10;
A = r.randn(n)+1j*r.randn(n);
[V,D] = eig(A);
d = diag(D, 0); % get eigenvalues in a vector
[~, I] = sort(real(d)); % get index of eigenvalues after sorting
D2 = diag(d(I)); % get sorted eigenvalues as diagonal matrix
V2 = V(:, I); % reorder eigenvectors to match sorted eigenvalues
any(any(abs(A - V2*D2*inv(V2)) > 1e-14)) % test sorted eigenvalues and eigenvectors