我在一个类中有一个函数,它在while循环中被调用。 此函数获取2个6x6矩阵并返回按中指定的维数放大的向量np.linspace公司,即6x91。我想将这三个数组作为浮点数连接在一个文件中(使用savetxt)。下面是一个与类分开运行的函数示例,它运行良好。 当我在循环之前声明列表输出,并通过Tkinter窗口更新EulArr,将数组追加到列表中时,问题就出现了。我只能用%s格式保存数组,但不能再按%f保存了。为什么?在
import numpy as np
from math import pi
M=100. #total estimated mass to move (upper platform+upper legs+payload)
g=9.81
nsteps= 91
Li=np.matrix([[-10.64569774, -93.1416122, 116.35191853],\
[-10.68368329, 93.17236065, 116.35542498],\
[85.985087,37.35196994, 116.20350534],[-75.34703551,-55.83790049, 115.44528196],\
[-75.33938926, 55.78964226, 115.44457613],[86.0307188,-37.33446016, 116.19929305]])
ti=np.matrix([[88.15843159,88.04450508,50.10006323, -138.28903445, -138.26610178,50.2369224],\
[-108.75675186, 108.84408749, 130.72504635, 21.82594871, -21.97569549,-130.6774372],\
[ 119.40585161, 119.40170883, 119.23577854, 118.41560138, 118.41643529,119.24075525]])
ti=ti.T #transpose the ti for compatible format
x_cm= 1.
y_cm= -1.
z_cm=87.752
z=np.linspace(0,90,nsteps)
Fx=-M*g*np.cos(pi*z/180)
Fy= M*g*np.sin(pi*z/180)
Fz=50 # including braking forces [N]
#specify here the centre of mass coordinates retrieved from Inventor
Mx=Fz*y_cm-Fy*z_cm
My=-Fz*x_cm+Fx*z_cm
Mz=Fy*x_cm-Fx*y_cm
mc=np.zeros((6,3),'float')
ex_forces=np.array([Fx,Fy,Fz,Mx,My,Mz])
for i in range(6):
mc[i,:]=np.cross(ti[i,:],Li[i,:])
r1=[Li[0,0],Li[1,0],Li[2,0],Li[3,0],Li[4,0],Li[5,0]]
r2=[Li[0,1],Li[1,1],Li[2,1],Li[3,1],Li[4,1],Li[5,1]]
r3=[Li[0,2],Li[1,2],Li[2,2],Li[3,2],Li[4,2],Li[5,2]]
r4=[mc[0,0],mc[1,0],mc[2,0],mc[3,0],mc[4,0],mc[5,0]]
r5=[mc[0,1],mc[1,1],mc[2,1],mc[3,1],mc[4,1],mc[5,1]]
r6=[mc[0,2],mc[1,2],mc[2,2],mc[3,2],mc[4,2],mc[5,2]]
DMatrix=np.vstack([r1,r2,r3,r4,r5,r6])
print 'DMatrix form:\n', DMatrix
invD=np.linalg.inv(DMatrix)
print ' inv(DMatrix) is:\n', invD
legF=np.dot(ex_forces,invD)
#slice it!
legF=legF.tolist()
a,b=np.shape(legF)
print 'check identity matrix:\n', np.dot(invD,DMatrix)
print 'leg forces:\n',legF, type(legF)
newlegF=np.reshape(legF,(1,a*b))
strokeValues= np.array([[-0.3595, .1450483, -0.3131,0.4210,-0.0825,.19124]])
print 'strokeValues shape:\n', np.shape(strokeValues)
print 'leg forces vector shape:', np.shape(newlegF)
EulArr=np.array([[0.12,0.2,0,-3.,-1.,15.]])
output=np.concatenate((strokeValues,EulArr,newlegF),axis=1)
np.savetxt('leg_forces.dat', output,fmt=' %f')
print output ,np.shape(output)
课程将如下所示:
^{pr2}$我得到以下错误:
np.savetxt('act_lengths.dat', output, fmt='%f')
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1047, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: float argument required, not numpy.ndarray
输出是一个包含
ndarray
s的list
。我认为在您的代码中,output
的元素必须是大小不同的ndarray
s,internallyNumPy由于数据不规则而未能将其转换为float数组,而是创建了一个object
s*的数组。我可以用但是如果我改为使用
output = [np.random.rand(3), np.random.rand(3)]
它就可以工作了。在(*)即
^{pr2}$相关问题 更多 >
编程相关推荐