This example shows how to create a MATLAB® array
in Python® and pass it as the input argument to the MATLAB
matlab package provides constructors
to create MATLAB arrays in Python. The MATLAB Engine
API for Python can pass such arrays as input arguments to MATLAB functions,
and can return such arrays as output arguments to Python. You
can create arrays of any MATLAB numeric or logical type from Python sequence
Create a MATLAB array from a Python
sqrt function on the array.
import matlab.engine eng = matlab.engine.start_matlab() a = matlab.double([1,4,9,16,25]) b = eng.sqrt(a) print(b)
The engine returns
b, which is a 1-by-5
Create a multidimensional array. The
returns a 2-D
matlab.double array to Python.
for loop to print each row on a separate
line. (Press Enter again when you
... prompt to close the loop and print.)
a = eng.magic(6) for x in a: print(x) ...
[35.0,1.0,6.0,26.0,19.0,24.0] [3.0,32.0,7.0,21.0,23.0,25.0] [31.0,9.0,2.0,22.0,27.0,20.0] [8.0,28.0,33.0,17.0,10.0,15.0] [30.0,5.0,34.0,12.0,14.0,16.0] [4.0,36.0,29.0,13.0,18.0,11.0]
tril function to get the lower
triangular portion of
a. Print each row on a separate
b = eng.tril(a) for x in b: print(x) ...
[35.0,0.0,0.0,0.0,0.0,0.0] [3.0,32.0,0.0,0.0,0.0,0.0] [31.0,9.0,2.0,0.0,0.0,0.0] [8.0,28.0,33.0,17.0,0.0,0.0] [30.0,5.0,34.0,12.0,14.0,0.0] [4.0,36.0,29.0,13.0,18.0,11.0]