arrayToTorchTensor
R2026bSyntax
Description
converts a MATLAB® numeric array to a PyTorch® Tensor.torchTensor = arrayToTorchTensor(matlabArray)
uses additional arguments to specify permutation, number of dimensions, and torch
datatype.torchTensor = arrayToTorchTensor(matlabArray,Name=Value)
Examples
Input Arguments
Name-Value Arguments
Output Arguments
Algorithms
arrayToTorchTensor determines the number of dimensions of the resulting
torch.Tensor using the following precedence (highest to lowest):
Explicit
NumDimensions. IfNumDimensionsis specified, it is used directly.DimensionOrder: IfDimensionOrderis specified,NumDimensionsis set to the length ofDimensionOrder.dlarrayinput. IfmatlabArrayis adlarray, the number of dimensions is set tondims(matlabArray).MATLAB–Python interface rules. If none of the above apply, dimensions are set to
size[a b …]→shape[a b …], except for these two cases:size[1 k]→shape[k]fork>1. In this case, the result is 1-Dtorch.Tensor.size[1 1]→shape[]. This is the empty shape, which corresponds to scalartorch.Tensor.
For more information, see Pass Data Between MATLAB and Python from MATLAB.
Extended Capabilities
Version History
Introduced in R2026b
See Also
Functions
torchTensorToArray|forward|addFunction|callFunction|export|reload|importNetworkFromPyTorch
Objects
dlarray|PyTorchModel|gpuArray(Parallel Computing Toolbox)
Topics
- Use PyTorch and MATLAB for Deep Learning Workflows
- Pass GPU Data Between MATLAB and Python (Parallel Computing Toolbox)