Main Content

pycupyarray

R2026b

Convert MATLAB array to Python CuPy array

Since R2026b

    Description

    cpa = pycupyarray(mla) converts the specified MATLAB® array to a cupy.ndarray. This function requires CuPy.

    Note

    The pycupyarray function supports sending data to Python® environments running in out-of-process execution mode only. To configure the Python interpreter to use out-of-process execution mode, call the pyenv function and set the ExecutionMode name-value argument to "OutOfProcess".

    example

    Examples

    collapse all

    Convert a gpuArray to a cupy.ndarray.

    X = rand(1000,"gpuArray");
    pyX = pycupyarray(X);

    First, convert a gpuArray to a cupy.ndarray.

    X = rand(1000,"gpuArray");
    pyX = pycupyarray(X);

    Operate on the CuPy array, on the GPU, using Python functions. Replace the call to myFunctions.proccessData with your desired Python functions.

    pyZ = py.myFunctions.processData(pyY)
    

    Transfer the resulting Python data back to MATLAB.

    Z = gpuArray(pyZ);
    

    Input Arguments

    collapse all

    Input array to convert, specified as a gpuArray object, a numeric array, or a logical array.

    Complex inputs must be single or double.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
    Complex Number Support: Yes

    Output Arguments

    collapse all

    Output array, returned as a cupy.ndarray.

    The data type of the array depends on the underlying type of the input array.

    MATLAB Input Array Underlying TypeResulting Python Data Type
    double

    float64

    single

    float32

    int8

    int8

    uint8

    uint8

    int16

    int16

    uint16

    uint16

    int32

    int32

    uint32

    uint32

    int64

    int64

    uint64

    uint64

    double (complex)

    complex128

    single (complex)

    complex64

    logical

    bool

    Limitations

    • Sparse arrays are not supported.

    Extended Capabilities

    expand all

    GPU Arrays
    Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

    Version History

    Introduced in R2026b