在Matlab中以相同的方式(顺序)计算python中的特征值 [英] Calculate eigen value in python as same way(order) in Matlab

查看:528
本文介绍了在Matlab中以相同的方式(顺序)计算python中的特征值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在V中返回特征向量和在D中返回特征值的Matlab代码。考虑C是9 * 9矩阵,则V是9 * 9矩阵,D是9 * 9对角线。矩阵。

This is the Matlab code which is returning eigenvector in V and eigenvalue in D. Consider C is 9*9 matrix then V is 9*9 matrix and D is 9*9 diagonal. matrix.

[V,D] = eig(C);

我希望Python中的内容与Matlab的顺序相同。我正在使用此代码:

I want the same thing in Python and in the same order as Matlab. I am using this code:

[V1, D] = np.linalg.eig(C)    
V = np.zeros((9,9))

for i in range(9):
    V[i][i] = V1[i]

(考虑V在for循环中)

(consider V to be in the for loop)

此代码给出我在V1中的特征值和D中的特征向量。我将V1改为V以获得对角9 * 9矩阵。

This code is giving me eigenvalue in V1 and eigenvector in D. I changed V1 to V to get a diagonal 9*9 matrix.

但问题是我想要特征值和向量与Matlab相同的顺序,我没有在python中获得。请帮我按照与Matlab相同的顺序获取值。

But the problem is that I want the eigenvalue and vector in the same order as Matlab which I am not getting in the python. Please help me in getting the values in the same order as Matlab.

请参阅下面的链接,了解Matlab和python之间的值差异。
https://drive.google.com/drive/folders/1zjhbKH0q_XXbBziZhfpL1- qS3B5oDuMb

See the link below for the difference in values between Matlab and python. https://drive.google.com/drive/folders/1zjhbKH0q_XXbBziZhfpL1-qS3B5oDuMb

推荐答案

Matlab将按升序将特征值输出到D矩阵的对角元素(即最低特征值)是D(1,1),最大的是D(9,9)。

Matlab will output the eigenvalues to the diagonal elements of the D matrix in ascending order (i.e. lowest eigenvalue is D(1,1) and the largest one is D(9,9)).

Python不遵循这个约定和输出(特征值和特征向量)必须用类似的东西排序;

Python doesn't follow this convention and the outputs (eigenvalues and eigenvectors) must be sorted with something like;

ind = np.argsort(V1);
V1 = V1[ind];
D = D[:,ind];

这篇关于在Matlab中以相同的方式(顺序)计算python中的特征值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆