MATLAB 到 Python 的数据类型映射

您所在的位置:网站首页 uint8转换为double MATLAB 到 Python 的数据类型映射

MATLAB 到 Python 的数据类型映射

2023-08-12 19:20| 来源: 网络整理| 查看: 265

MATLAB 到 Python 的数据类型映射

当调用 Python® 函数时,MATLAB® 将 MATLAB 数据转换为最适合在 Python 语言中表达该数据的类型。

将标量值传递给 Python

MATLAB 输入参数类型 - 仅标量值

生成的 Python py. 类型

示例

double(实数) single(实数)

float

在 MATLAB 中使用 Python 数值变量

double(复数) single(复数)

complex

z = complex(1,2); py.cmath.polar(z) ans = Python tuple with no properties. (2.23606797749979, 1.1071487177940904)

int8 uint8 int16 uint16 int32

int

 

uint32 int64 uint64

int long(仅限 2.7 版本)

 

NaN

float("nan")

 

Inf

float("inf")

 

string 标量 char 向量

str

在 MATLAB 中使用 Python str 变量

string 中的 值

None

py.list({string(missing),'Value'}) ans = Python list with no properties. [None, 'Value']

logical

bool

 

结构体

dict

在 MATLAB 中使用 Python dict 变量

Python 对象 - py.type

type

 

函数句柄 @py.module.function(仅限于指向 Python 函数的函数句柄)

module.function

Pass Python Function to Python map Function将向量传递给 Python

MATLAB 输入参数类型 - 1 ×N 向量

生成的 Python 类型

double(实数)

array.array('d')

single(实数)

array.array('f')

int8(实数)

array.array('b')

uint8(实数)

array.array('B')

int16(实数)

array.array('h')

uint16(实数)

array.array('H')

int32(实数)

array.array('i')

uint32(实数)

array.array('I')

int64(实数)- 不支持 Windows® 上的 Python 2.7

array.array('q')

uint64(实数)- 不支持 Windows 上的 Python 2.7

array.array('Q')

double(复数) single(复数) int8(复数) uint8(复数) int16(复数) uint16(复数) int32(复数) uint32(复数)

memoryview

logical

memoryview

char 向量 string 标量

str

包含大于 127 的值的 char 数组(仅限版本 2.7)

unicode

cell 向量

tuple

将矩阵和多维数组传递给 Python

Python 语言提供访问内存缓冲区(如存储在 MATLAB 数组中的数据)的协议。MATLAB 为 MATLAB 数组实现此 Python 缓冲区协议,以便直接从 Python 代码中读取 MATLAB 数组,从而在与 MATLAB 相同的进程中运行,而无需复制数据。

许多 Python 函数直接从 Python 使用 MATLAB 数组,而不将其转换为原生 Python 类型。某些函数可能需要特定类型,例如 numpy.ndarray,或可能会修改数组中的数据。这些函数可能接受 MATLAB 数组并将数据复制到所需的类型中。其他函数则不然,如果您未传递所需的类型,可能就会显示错误。要将数据传递给这些函数,请首先从 MATLAB 数据创建所需的 Python 类型,然后将其传递给 Python 函数。例如,要创建数组 p 以传递给需要 numpy.array 类型数据的 Python 函数,请键入:

p = py.numpy.array(magic(3))p = Python ndarray: 8 1 6 3 5 7 4 9 2 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.

在 Python 中不支持 MATLAB 稀疏数组。请参阅不支持的 MATLAB 类型。

参数错误故障排除

如果 Python 函数需要特定的 Python 多维数组类型,例如 numpy.ndarray,MATLAB 将显示消息,并提示应该如何继续。如果问题产生的原因有可能是将矩阵或多维数组作为参数传递,请执行以下操作。

查阅 Python 函数的文档,找出参数需要的类型。

在 MATLAB 中创建该类型的 Python 对象,并将其传递给 Python 函数。

例如,假设以下代码返回错误。

a = [1 2; 3 4]; py.pyfunc(a)

如果 pyfunc 的文档指定需要的类型为 numpy.ndarray,请尝试进行以下转换:

py.pyfunc(numpy.ndarray(a))

如果错误仍然存在,请通过查看 Python 异常中的其他信息来确定根本原因。

自动将 Python 类型转换为 MATLAB 类型

MATLAB 自动将从 Python 函数返回的下列数据类型转换为 MATLAB 类型。要转换其他类型,请参阅将 Python 类型显式转换为 MATLAB 类型。

Python 返回类型,如 Python 中所显示

生成的 MATLAB 类型 - 标量

float

double

complex

复数 double

int(仅限版本 2.7)。

对于 Python 版本 3.x int,您必须显式转换。请参阅 将 Python 类型显式转换为 MATLAB 类型。

int64

bool

logical

所有其他 Python 类型 - type

Python 对象 - py.type

将 Python 类型显式转换为 MATLAB 类型

如果 Python 函数的输出实现 Python 缓冲区协议(如 numpy.ndarray)并且是数值或逻辑值,则 MATLAB 显示:

实际的 Python 类型

底层数据

对应的 MATLAB 转换函数。使用此函数可将 Python 对象完全转换为 MATLAB 数组。

使用下列 MATLAB 函数将 Python 数据类型转换为 MATLAB 类型。

Python 返回类型或协议,如在 MATLAB 中所显示

MATLAB 转换函数

示例

py.str(版本 3.x)

string char

在 MATLAB 中使用 Python str 变量

py.str(版本 2.7)

string char uint8

 

py.unicode

string char

 

使用 __str__ 方法的对象

char

py.help('datetime.date.__str__') Help on wrapper_descriptor in datetime.date: datetime.date.__str__ = __str__(self, /) Return str(self). d = py.datetime.date(... int32(2020),int32(3),int32(4)); char(d) ans = '2020-3-04'

py.int py.long py.float py.bool

数值函数: double single int8 uint8 int16 uint16 int32 uint32 int64 uint64

 logical

py.bytes

uint8

 

py.array.array py.numpy.ndarray py.memoryview

您可以将任何格式的 py.array.array 和 py.memoryview 对象转换为所需的 MATLAB 类型。

数值函数: double single int8 uint8 int16 uint16 int32 uint32 int64 uint64

在 MATLAB 中使用 Python 数值变量,例如,在 MATLAB 中使用 Python 整数数组类型。

py.list 和 py.tuple

double single int8 uint8 int16 uint16 int32 uint32 int64 uint64 logical string cell

在 MATLAB 中使用 Python list 变量 在 MATLAB 中使用 Python tuple 变量

有关详细信息,请参阅Error Converting Elements of list or tuple。

映射协议;例如,py.dict

struct

在 MATLAB 中使用 Python dict 变量

例如,Python 函数返回此数组 p:

p = Python ndarray: 8 1 6 3 5 7 4 9 2 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.

您可以通过键入以下内容将其转换为 MATLAB 矩阵 P:

P = double(p)P = 3×3 8 1 6 3 5 7 4 9 2

如果需要 p 的 Python 属性的特定信息,请键入:

details(p) py.numpy.ndarray handle with properties: T: [1×1 py.numpy.ndarray] base: [1×1 py.NoneType] ctypes: [1×1 py.numpy.core._internal._ctypes] data: [1×3 py.memoryview] dtype: [1×1 py.numpy.dtype[float64]] flags: [1×1 py.numpy.flagsobj] flat: [1×1 py.numpy.flatiter] imag: [1×1 py.numpy.ndarray] itemsize: [1×1 py.int] nbytes: [1×1 py.int] ndim: [1×1 py.int] real: [1×1 py.numpy.ndarray] shape: [1×2 py.tuple] size: [1×1 py.int] strides: [1×2 py.tuple] Methods, Events, Superclasses

如果 Python 模块在其 __doc__ 特性中提供内容,MATLAB 将链接到该信息。

不要使用 Python 对象作为字典的键

您无法将 Python 对象作为键参数传递给 MATLAB dictionary 函数,也无法将其作为输入传递给 keyMatch 函数。

不支持的 MATLAB 类型

Python 不支持下列 MATLAB 类型。

多维 char 或 cell 数组

结构体数组

稀疏数组

categorical、 table、 containers.Map、 datetime 类型

MATLAB 对象

meta.class (py.class)

相关主题在 MATLAB 中使用 Python 数值变量在 MATLAB 中使用 Python str 变量在 MATLAB 中使用 Python list 变量在 MATLAB 中使用 Python tuple 变量在 MATLAB 中使用 Python dict 变量


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3