繁体   English   中英

将双数转换为浮点数

[英]Convert double number to float number

我需要对Python中的MATLAB数据进行详细说明。 数据以doubles数组形式存储在Matlab中。 当我检索它时,尽管在这里说过,当由Python处理时,来自Matlab的double数据类型被转换为float数据类型,但出现此错误:

TypeError:不可排序的类型: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数转换为float以便可以将其与其他Python变量一起使用?

在Matlab中将double转换为float就像调用single函数一样简单:

A = rand(10);
whos A;

B = single(A);
whos B;

根据控制台输出:

  Name       Size            Bytes  Class     Attributes

  A         10x10              800  double              

  Name       Size            Bytes  Class     Attributes

  B         10x10              400  single 

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

编辑

由于您无法操纵Matlab数据,因此,为了完成此操作,我建议您使用任一Numpy(以防万一,请参考此函数: https : //docs.scipy.org/doc/numpy-1.12.0/reference/ generate / numpy.ndarray.astype.html )或用于直接转换的struct (以防万一,请参考此答案: 在Python中将double转换为float )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM