MATLAB Arrays as Python Variables

The matlab Python® package provides array classes to represent arrays of MATLAB® numeric types as Python variables.

Create MATLAB Arrays in Python

You can create MATLAB numeric arrays in a Python session by calling constructors from the matlab Python package (for example, matlab.double, matlab.int32). The name of the constructor indicates the MATLAB numeric type. You can pass MATLAB arrays as input arguments to MATLAB functions called from Python. When a MATLAB function returns a numeric array as an output argument, the array is returned to Python.

You can initialize the array with an optional initializer input argument that contains numbers. The initializer argument must be a Python sequence type such as a list or a tuple. The optional size input argument sets the size of the initialized array. To create multidimensional arrays, specify initializer to contain multiple sequences of numbers, or specify size to be multidimensional. You can create a MATLAB array of complex numbers by setting the optional is_complex keyword argument to True. The mlarray module provides the MATLAB array constructors listed in the table.

Class from matlab Package

Constructor Call in Python

MATLAB Numeric Type

matlab.double

matlab.double(
initializer=None,
size=None,
is_complex=False)

Double precision

matlab.single

matlab.single(
initializer=None,
size=None,
is_complex=False)

Single precision

matlab.int8

matlab.int8(
initializer=None,
size=None,
is_complex=False)

8-bit signed integer

matlab.int16

matlab.int16(
initializer=None,
size=None,
is_complex=False)

16-bit signed integer

matlab.int32

matlab.int32(
initializer=None,
size=None,
is_complex=False)

32-bit signed integer

matlab.int64[a]

matlab.int64(
initializer=None,
size=None,
is_complex=False)

64-bit signed integer

matlab.uint8

matlab.uint8(
initializer=None,
size=None,
is_complex=False)

8-bit unsigned integer

matlab.uint16

matlab.uint16(
initializer=None,
size=None,
is_complex=False)

16-bit unsigned integer

matlab.uint32

matlab.uint32(
initializer=None,
size=None,
is_complex=False)

32-bit unsigned integer

matlab.uint64[b]

matlab.uint64(
initializer=None,
size=None,
is_complex=False)

64-bit unsigned integer

matlab.logical

matlab.logical(
initializer=None,
size=None)[c]

Logical

[a] In Python 2.7 on Windows, matlab.int64 is converted to int32 in MATLAB. Also, MATLAB cannot return an int64 array to Python.

[b] In Python 2.7 on Windows, matlab.uint64 is converted to uint32 in MATLAB. Also, MATLAB cannot return a uint64 array to Python.

[c] Logicals cannot be made into an array of complex numbers.

When you create an array with N elements, the size is 1-by-N because it is a MATLAB array.

import matlab
A = matlab.int8([1,2,3,4,5])
print(A.size)

(1, 5)

The initializer is a Python list containing five numbers. The MATLAB array size is 1-by-5, indicated by the tuple (1,5).

MATLAB Array Attributes and Methods in Python

All MATLAB arrays created with matlab package constructors have the attributes and methods listed in the table below:

Attribute or Method

Purpose

size

Size of array returned as a tuple

reshape(size)

Reshape the array as specified by the sequence size

Multidimensional MATLAB Arrays in Python

In Python, you can create multidimensional MATLAB arrays of any numeric type. Use two Python lists of floats to create a 2-by-5 MATLAB array of doubles.

import matlab
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 size attribute of A shows it is a 2-by-5 array.

print(A.size)

(2, 5)

Index Into MATLAB Arrays in Python

You can index into MATLAB arrays just as you can index into Python lists and tuples.

import matlab
A = matlab.int8([1,2,3,4,5])
print(A[0])

[1,2,3,4,5]

The size of the MATLAB array is (1,5); therefore, A[0] is [1,2,3,4,5]. Index into the array to get 3.

print(A[0][2])

3

Python indexing is zero-based. When you access elements of MATLAB arrays in a Python session, use zero-based indexing.

This example shows how to index into a multidimensional MATLAB array.

A = matlab.double([[1,2,3,4,5], [6,7,8,9,10]])
print(A[1][2])

8.0

Slice MATLAB Arrays in Python

You can slice MATLAB arrays just as you can slice Python lists and tuples.

import matlab
A = matlab.int8([1,2,3,4,5])
print(A[0][1:4])

[2,3,4]

You can assign data to a slice. This example shows an assignment from a Python list to the array.

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 in this example.

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)

[[1,2,30,40,5,6,70,80]]

    Note:   Slicing MATLAB arrays behaves differently from slicing a Python list. Slicing a MATLAB array returns a view instead of a shallow copy.

    Given a MATLAB array and a Python list with the same values, assigning a slice results in different results.

    >>>mlarray = matlab.int32([[1,2],[3,4],[5,6]])
    >>>py_list = [[1,2],[3,4],[5,6]]
    >>>mlarray[0] = mlarray[0][::-1]
    >>>py_list[0] = py_list[0][::-1]
    >>>mlarray[0]
    matlab.int32([[2,2],[3,4],[5,6]])
    >>>py_list
    [[2,1],[3,4],[5,6]]

Reshaping MATLAB Arrays in Python

You can reshape a MATLAB array in Python with the reshape method. The input argument, size, must be a sequence that does not change the number of elements in the array. Use reshape to change a 1-by-9 MATLAB array to 3-by-3.

import matlab
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 Examples

Was this topic helpful?