首页 > 解决方案 > 将numpy数组转换为整数数组

问题描述

我有数组

length = 7
angle = 30  
x = [1.21660254e+02, 1.71660254e+02, 2.38660254e+02, 2.05660254e+02]

我试过这个

P2x =  int(np.round(x + length * math.cos(math.radians(-angle))))

给我错误TypeError: only size-1 arrays can be converted to Python scalars

标签: pythonarraysnumpy

解决方案


math如果您已经在使用,则不需要该模块numpy

import numpy as np

length = 7
angle = 30 
x = [1.21660254e+02, 1.71660254e+02, 2.38660254e+02, 2.05660254e+02]

xP2x = np.round(np.array(x) + length * np.cos(np.radians(-angle))).astype(int)
print(xP2x)

出去:

[128 178 245 212]

推荐阅读