Matlab array to python array
The Show MATLAB Classes in the matlab.engine Python Module
Properties and Methods of MATLAB Classes in the matlab Python PackageAll MATLAB arrays created with Properties
Methods
Multidimensional MATLAB Arrays in PythonIn Python, you can create multidimensional MATLAB arrays of any numeric type. Use two Python import matlab.engine A = matlab.double([[1,2,3,4,5], [6,7,8,9,10]]) print(A) [[1.0,2.0,3.0,4.0,5.0],[6.0,7.0,8.0,9.0,10.0]] The Index Into MATLAB Arrays in PythonYou can index into MATLAB arrays just as you can index into Python import matlab.engine A = matlab.int8([1,2,3,4,5]) print(A[0]) The size of the MATLAB array is Python indexing is zero-based. When you access elements of MATLAB arrays in a Python session, use zero-based indexing. Index into a multidimensional MATLAB array. A = matlab.double([[1,2,3,4,5], [6,7,8,9,10]]) print(A[1][2]) Slice MATLAB Arrays in PythonYou can slice MATLAB arrays the same way you slice Python import matlab.engine A = matlab.int8([[1,2,3,4,5]]) print(A[0][1:4]) You can assign data to a slice. This code shows assignment from a Python A = matlab.double([[1,2,3,4],[5,6,7,8]]); A[0] = [10,20,30,40] print(A) [[10.0,20.0,30.0,40.0],[5.0,6.0,7.0,8.0]] You can assign data from another MATLAB array, or from any Python iterable that contains numbers. You can specify slices for assignment as shown here. A = matlab.int8([1,2,3,4,5,6,7,8]); A[0][2:4] = [30,40] A[0][6:8] = [70,80] print(A) Reshape MATLAB Arrays in PythonYou can reshape a MATLAB array in Python with the import matlab.engine A = matlab.int8([1,2,3,4,5,6,7,8,9]) A.reshape((3,3)) print(A) [[1,4,7],[2,5,8],[3,6,9]] Related Topics
|