The MATLAB Blog

Practical Advice for People on the Leading Edge

Numpy in MATLAB

I've been a user of Python for almost as long as I've been a user of MATLAB and am very comfortable with both ecosystems. As such, I have been delighted with the steadily improving interoperability between the two languages. It is extremely easy to call MATLAB code from Python and Python code from MATLAB. I use this all of the time and a particularly nice demonstration of this is the work I did with Prof. Stefan Güttel of University of Manchester where calling Python code from MATLAB was the first step in The CLASSIX Story: Developing the Same Algorithm in MATLAB and Python Simultaneously. I'll write about this in the near future but, for now, I encourage you to click on the link and watch the presentation video.
I thought it would be instructive to quickly demonstrate how easy this integration is becoming. I'll assume you've installed Python along with Numpy and made this available to MATLAB. Details on how to do this are in the documentation: Configure Your System to Use Python.

Passing Numpy arrays to MATLAB functions

Let's create a random Numpy array using numpy.random.random_sample(). One detail to be aware here of is the use of int64. The Numpy function expects integers whereas all numbers in MATLAB are of type double by default so a conversion is necessary.
npyArray = py.numpy.random.random_sample(int64([5,5]))
npyArray =
Python ndarray: 0.8579 0.4650 0.0921 0.2357 0.7336 0.3645 0.8259 0.0651 0.7572 0.5455 0.6750 0.2944 0.7437 0.5455 0.6160 0.2285 0.4557 0.5403 0.7857 0.9258 0.5544 0.7524 0.3155 0.3983 0.1226 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.
To pass this to a MATLAB function, simply wrap it in double to do the conversion
result = eig(double(npyArray))
result = 5×1 complex
2.5493 + 0.0000i 0.6249 + 0.2510i 0.6249 - 0.2510i -0.2316 + 0.0636i -0.2316 - 0.0636i

Passing MATLAB arrays to Numpy

Similarly I could done this the other way around and create the random matrix using MATLAB
matlabArray = rand(5)
matlabArray = 5×5
0.3015 0.6665 0.0326 0.3689 0.6448 0.7011 0.1781 0.5612 0.4607 0.3763 0.6663 0.1280 0.8819 0.9816 0.1909 0.5391 0.9991 0.6692 0.1564 0.4283 0.6981 0.1711 0.1904 0.8555 0.4820
lets get Numpy to compute the eigenvalues. Here, no conversion is necessary. I can simply pass matlabArray to the Numpy function as if it were a Numpy array
pyEig = py.numpy.linalg.eigvals(matlabArray)
pyEig =
Python ndarray: 2.4572 + 0.0000i 0.5883 + 0.0000i -0.4283 + 0.1432i -0.4283 - 0.1432i -0.1891 + 0.0000i Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.
The results from some Numpy functions can be a little more complicated but they are usually straightforward to deal with. Here, for example the result is a Python object
pyEigensystem = py.numpy.linalg.eig(matlabArray)
pyEigensystem =
Python EigResult with properties: eigenvalues eigenvectors EigResult(eigenvalues=array([ 2.45720028+0.j , 0.58829742+0.j , -0.42827389+0.14322908j, -0.42827389-0.14322908j, -0.18906899+0.j ]), eigenvectors=array([[-0.34571704+0.j , 0.50185891+0.j , -0.1340401 +0.06114006j, -0.1340401 -0.06114006j, -0.37491139+0.j ], [-0.40874583+0.j , -0.02017228+0.j , -0.12589034-0.16783168j, -0.12589034+0.16783168j, -0.28001004+0.j ], [-0.53857747+0.j , -0.74738581+0.j , 0.44874861+0.02367601j, 0.44874861-0.02367601j, 0.41023205+0.j ], [-0.4939665 +0.j , -0.19055504+0.j , -0.61069499+0.j , -0.61069499-0.j , -0.29737597+0.j ], [-0.42348314+0.j , 0.39093982+0.j , 0.59499431+0.07332683j, 0.59499431-0.07332683j, 0.72409462+0.j ]]))
You can get to the results as you might expect
eigValues = pyEigensystem.eigenvalues
eigValues =
Python ndarray: 2.4572 + 0.0000i 0.5883 + 0.0000i -0.4283 + 0.1432i -0.4283 - 0.1432i -0.1891 + 0.0000i Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.
Versions
Everything shown here was using MATLAB R2024b, Python 3,12 and Numpy 2.1.1
Over to you
Have you tried the MATLAB/Python interoperability? How did you find it?
|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.