首页 > 解决方案 > 使用python在numpy矩阵中格式化

问题描述

。倾倒

每个.dump文件将具有与上图类似的格式。我已阅读 stackoverflow 建议的链接。我在 numpy 矩阵中遇到了问题,我的格式与预期的完全不同。我需要一个 4x4 大小 4x1 大小矩阵的 np.matrix。矩阵内的值是某些浮点值。

#%%
def q_rot_trans(a,b,c,d,x1,x2,y1,y2,z1,z2):
    '''Calculates the Rotation Matrix from Quaternion
    a is the real part
    b, c, d are the complex elements'''

    'defining Rotation vector'

    R11 = (a**2+b**2-c**2-d**2)
    R12 = 2.0*(b*c-a*d)
    R13 = 2.0*(b*d+a*c)

    R21 = 2.0*(b*c+a*d)
    R22 = a**2-b**2+c**2-d**2
    R23 = 2.0*(c*d-a*b)

    R31 = 2.0*(b*d-a*c)
    R32 = 2.0*(c*d+a*b)
    R33 = a**2-b**2-c**2+d**2

    'defining Translation vector'

    T11 = 1
    T12 = 0
    T13 = 0
    T14 = 0

    delta_x = x2 - x1
    delta_y = y2 - y1
    delta_z = z2 - z1

    T21 = delta_x
    T31 = delta_y
    T41 = delta_z

    'defining point vector'
    v11 = 1
    v12 = x1
    v13 = y1
    v14 = z1
    old_vector = np.matrix([[v11], [v12], [v13], [v14]])

    rot_trans = np.matrix([[T11, T12, T13, T14], [T21, R11, R12, R13], [T31, R21, R22, R23], [T41, R31, R32, R33]])

    'Finding new vector after transformation and rotation'
#    new_vector = np.matmul(rot_trans,old_vector).shape
    print(v11,v12,v13,v14,T11,T12,T13,T14,T21)
#    print('Rotation and Transformation matrix is: \n',rot_trans)
#    print('old vector is: \n', old_vector)
#    print('Transformed vector: ', new_vector)
    return 
#%%
i = pd.read_excel('paketone28000.dump.xlsx')  ##to read the excel file format

x1 = i['x']##import centre of mass from excel file format
y1 = i['y']
z1 = i['z']

q1 = i['q1'] ##attaining quaternons from excel file format. (comma(,) transformed series to tuple)
q2 = i['q2']
q3 = i['q3']
q4 = i['q4']
#%%

j = pd.read_excel('paketone32000.dump.xlsx')  ##to read the excel file format

x2 = j['x']##import centre of mass from excel file format
y2 = j['y']
z2 = j['z']
#%%

#print ('x1: ',x1, 'y1: ',y1, 'z1: ',z1, 'q1: ',q1, 'q2: ',q2, 'q3: ',q3, 'q4: ',q4, 'x2: ',x2,  'y2: ', y2, 'z2: ',z2)

#
q_rot_trans(q1,q2,q3,q4,x1,x2,y1,y2,z1,z2 )

我得到的结果如下:

Rotation and Transformation matrix is: 
 [[1 0 0 0]
 [0    0.00001
Name: x, dtype: float64 -15.191110999999998 15.477 41.7978]
 [0    0.000058
Name: y, dtype: float64 44.516999999999996
  3.0911109999999997 15.034799999999997]
 [0   -0.006693
Name: z, dtype: float64 2.197800000000001 44.3652
  -15.628889000000003]]
old vector is: 
 [[1]
 [0    2.10005
Name: x, dtype: float64]
 [0   -0.101145
Name: y, dtype: float64]
 [0    0.739066
Name: z, dtype: float64]]

但是预期的结果应该是大小分别为 4x4 和 4x1 的矩阵格式,这两个矩阵要相乘并且必须得到一个 4x1 大小的矩阵。

标签: pythonpandasnumpymatrix

解决方案


推荐阅读