Calculate eigen value in python as same way(order) in Matlab

2024/11/16 14:40:49

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);

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]

(consider V to be in the for loop)

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

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.

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

Answer

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 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];
https://en.xdnf.cn/q/119522.html

Related Q&A

Python: ctypes and Pointer to Structure

I am trying to make a pointer of a struct and then de-reference it. But its crashing. I have mimiced the behvior here with this simple code. from ctypes import * import ctypesclass File(Structure):_fie…

Python:Why readline() function doesnt work for file looping

I have the following code:#!/usr/bin/pythonf = open(file,r)for line in f:print line print Next line , f.readline() f.close()This gives the following output:This is the first lineNext line That was the …

How to replace cropped rectangle in opencv?

I have managed to cropped a bounding box with text, e.g. given this image:Im able to exact the following box:with this code: import re import shutilfrom IPython.display import Imageimport requests impo…

How can I read hexadecimal data with python?

I have this c# app that Im trying to cooperate with a app written in python. The c# app send simple commands to the python app, for instance my c# app is sending the following:[Flags]public enum GameRo…

Want to scrape all the specific href from the a tag

I have search the specific brand Samsung , for this number of products are search ,I just wanted to scrape all the href from the of the search products with the product name . enter code here import u…

Encryption code in def function to be written in python

need some help in the following code as it goes into infinite loop and does not validate user input: the get_offset is the function. Just edited need some help with the encryption part to be done in a …

Creating xml from MySQL query with Python and lxml

I am trying to use Python and LXML to create an XML file from a Mysql query result. Here is the format I want.<DATA><ROW><FIELD1>content</FIELD1><FIELD2>content</FIELD2…

How to add another iterator to nested loop in python without additional loop?

I am trying to add a date to my nested loop without creating another loop. End is my list of dates and end(len) is equal to len(year). Alternatively I can add the date to the dataframe (data1) is that …

How to know where the arrow ends in matplotlib quiver

I have programmed plt.quiver(x,y,u,v,color), where there are arrows that start at x,y and the direction is determined by u,v. My question is how can I know exactly where the arrow ends?

how send text(sendkey) to ckeditor in selenium python scripting -chrome driver

I cant send a text to the text box of CKEditor while I scripting.it not shown in the seleniumIDE recording also.Help me to fix this issueASAP