猿问

在 python3 中将 matlab.double 转换为整数或浮点数

我有一个由 Matlab 生成的包,可以在 python 中使用。输出是这样的:


print(IdOut, 'len : ', len(IdOut))

IdOut当我询问答案的类型时: <class 'mlarray.double'>.

例如其中之一的类型IdOut[0]是:<class 'mlarray.double'>


>>> type(IdOut)

# <class 'mlarray.double'>

>>> type(IdOut[0])

# <class 'mlarray.double'>

>>> idout[0]

# matlab.double([261.0])

我的问题是我希望这些数字是整数或浮点数。当我尝试时出现这些错误:


>>> float(IdOut[0])

# Traceback (most recent call last):

  File "<pyshell#8>", line 1, in <module>

    float(IdOut[0])

TypeError: float() argument must be a string or a number, not 'double'


>>> int(matlab.double([39400.0]))

# Traceback (most recent call last):

  File "<pyshell#4>", line 1, in <module>

    int(matlab.double([39400.0]))

TypeError: int() argument must be a string, a bytes-like object or a number, not 'double'

有人能告诉我如何将 的类型转换和更改mlarray.double为浮点数或整数吗?任何帮助将不胜感激。


慕雪6442864
浏览 341回答 2
2回答

交互式爱情

你可以使用numpy.array:from numpy import arraya = array(IdOut)print(a[0])print(int(a[0]))输出:array([261.])261

当年话下

我不知道这是否有帮助。但我可以说,如果你将你的转换mlarray.double为库中的数组numpy,那么你可以按照你想要的方式使用它。import numpy as npIdOut = np.array(IdOut)现在你可以用整数列出它:IdOut_list = [IdOut[i] for i in range(0, len(IdOut))]
随时随地看视频慕课网APP

相关分类

Python
我要回答