Contenido principal

torchTensorToArray

R2026b

Convert PyTorch Tensor to MATLAB numeric array

Since R2026b

Description

matlabArray = torchTensorToArray(torchTensor) converts a PyTorch® Tensor to a MATLAB® numeric array.

example

matlabArray = torchTensorToArray(torchTensor,Name=Value) uses additional arguments to specify permutation and MATLAB datatype.

example

Examples

collapse all

Use the DimensionOrder argument to reorder tensor dimensions during conversion to a MATLAB array.

Create a 3-D torch.Tensor of type float32.

T = arrayToTorchTensor(rand(2,3,4),DataType="float32");
T.shape
ans = 
  Python Size with no properties.
    torch.Size([2, 3, 4])
T.dtype
ans =
 
  Python dtype with properties:
           is_complex: 0
    is_floating_point: 1
            is_signed: 1
             itemsize: [1×1 py.int]

    torch.float32

Convert the tensor to a MATLAB array, reversing the dimension order.

A = torchTensorToArray(T,DimensionOrder=[3 2 1]);
size(A)
ans = 1×3
     4     3     2
class(A)
ans =
    'single'

Convert a PyTorch tensor that has a trailing singleton dimension. The trailing singleton is not preserved in the MATLAB output.

Create a 3-D tensor with a trailing singleton dimension.

T = arrayToTorchTensor(rand(1,2,3),DimensionOrder=[2 3 1]);
T.shape
ans = 
  Python Size with no properties.
    torch.Size([ 2, 3, 1])

Convert to a MATLAB array. The trailing singleton dimension is dropped in the output.

A = torchTensorToArray(T,DimensionOrder=[2 3 1]);
size(A)
ans = 1×2
     2     3

By default, a 1-D torch.Tensor of shape [k] becomes a MATLAB row vector of size [1 k].

Create a 1-D tensor.

T = arrayToTorchTensor(rand(1,5));
T.shape
ans = 
  Python Size with no properties.
    torch.Size([2, 3, 1])

Convert without additional arguments. The result is a row vector.

A = torchTensorToArray(T);
size(A)
 ans = 1×2
     2     3

Use DimensionOrder to convert a 1-D torch.Tensor to a MATLAB column vector instead of the default row vector.

Create a 1-D tensor.

T = arrayToTorchTensor(rand(1,5));
T.shape
ans = 
  Python Size with no properties.
    torch.Size([5])

Specify DimensionOrder=[1 2] to produce a column vector. The identity permutation preserves the dimension mapping while enforcing 2-D output with the tensor elements along the first dimension.

A = torchTensorToArray(T,DimensionOrder=[1 2]);
size(A)
ans = 1×2
     5     1

Input Arguments

collapse all

PyTorch Tensor, specified as a MATLAB object of class py.torch.Tensor.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: A = torchTensorToArray(T,DimensionOrder=[4 3 1 2])

Permutation vector to define ordering of dimensions in resulting MATLAB array, specified as a numeric row vector. The permutation applies to PyTorch Tensor with dimensions 1,2,3,... to make its dimension ordering match what is required by the MATLAB array.

If you do not specify the dimension order, torchTensorToArray uses shape[a b …]→size[a b …], except for these cases:

  • shape[k] →size[1 k] for k>1

  • shape[]→ size[1 1]. This is the empty shape, which corresponds to scalar torch.Tensor.

For more information, see Pass Data Between MATLAB and Python from MATLAB.

Example: DimensionOrder=[4 3 1 2]

MATLAB data type for resulting array, specified as a string corresponding to one of the supported datatypes. If you do not specify a data type, torchTensorToArray uses the MATLAB–Python® interface conventions. For more information, see Pass Data Between MATLAB and Python from MATLAB.

Example: DataType="double"

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Output Arguments

collapse all

MATLAB array, returned as a numeric array, logical array, or gpuArray if the device attribute of torchTensor is "cuda*". Using GPU requires Parallel Computing Toolbox™.

Extended Capabilities

expand all

Version History

Introduced in R2026b