将双精度数字转换为浮点数

2024-06-16 13:11:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在Python中对MATLAB数据做一些详细说明。 数据以doubles的数组形式存储在Matlab中。当我检索它时,尽管声明here在Python处理时,来自Matlab的double数据类型被转换成float数据类型,但我得到了以下错误:

TypeError: unorderable types: double() < float()

我想做的是

import matlab.engine

eng=matlab.engine.connect_matlab()

x = eng.workspace['MyData']
x = x[len(x)-1]
if x < 0.01:
    #do stuff

如何将存储在数组中的double编号转换为float,以便与其他Python变量一起使用?在


Tags: 数据声明here错误数组floatengengine
1条回答
网友
1楼 · 发布于 2024-06-16 13:11:41

在Matlab中将双精度数转换为浮点值非常简单,只需调用single function

A = rand(10);
whos A;

B = single(A);
whos B;

根据控制台输出:

^{pr2}$

由于要将64位数值转换为32位数值,请注意精度的损失。在

编辑

因为您不能操作您的Matlab数据,为了完成这一点,我建议您使用Numpy(在case:https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.ndarray.astype.html)或{}进行直接转换(在case:convert double to float in Python中参考这个答案)。在

相关问题 更多 >