python矩阵乘法广播错误

时间:2020-03-16 17:28:15

标签: python python-3.x numpy array-broadcasting

我试图在同一行中乘以5个矩阵并得到以下错误,

ValueError                                Traceback (most recent call last)
<ipython-input-32-4809be28729a> in <module>
     95   u_prev = np.transpose(u[t-1, :]).reshape(-1,1)
     96 
---> 97   x[t] = np.mat(A)*np.mat(x_prev) + np.mat(B)*np.mat(u_prev) + np.mat(process_noise)
ValueError: could not broadcast input array from shape (9,1) into shape (9)

这是代码段:有多个数组A,B,x_prev,u_prev和随机噪声。只需将它们相乘即可。

A = np.matrix([[1, 0, 0, ts, 0, 0, 1/2*ts**2, 0, 0],
               [0, 1, 0, 0, ts, 0, 0, 1/2*ts**2, 0],
               [0, 0, 1, 0, 0, ts, 0, 0, 1/2*ts**2],
               [0, 0, 0, 1, 0, 0, ts, 0, 0],
               [0, 0, 0, 0, 1, 0, 0, ts, 0],
               [0, 0, 0, 0, 0, 1, 0, 0, ts],
               [0, 0, 0, 0, 0, 0, 1, 0, 0],
               [0, 0, 0, 0, 0, 0, 0, 1, 0],
               [0, 0, 0, 0, 0, 0, 0, 0, 1]])

B = np.zeros((9, 3))

# --- update the state
process_noise = 0 + np.sqrt(Q)*np.random.uniform(-1,1)
process_noise = np.transpose(np.matrix(process_noise))

t = np.arange(0,5,0.1)
u = np.zeros((len(t), 3))
x = np.zeros((len(t), 9))

for t in range(1,t.size):   
    u[0, :] = np.array([Fx, Fy, Fz]) # set initial input
    u[t, :] = np.array([Fx, Fy, Fz])
    x_prev = np.transpose(x[t-1, :]).reshape(-1,1)
    u_prev = np.transpose(u[t-1, :]).reshape(-1,1)
    x[t] = np.mat(A)*np.mat(x_prev) + np.mat(B)*np.mat(u_prev) + np.mat(process_noise)

我也尝试过x[t] = A * x_prev + B * u_prev + process_noise,但遇到相同的错误。

1 个答案:

答案 0 :(得分:0)

这是在循环中矩阵乘法的正确方法:

In [74]: A = np.arange(9).reshape(3,3)                                                                               
In [75]: x = np.zeros((3,3))                                                                                         
In [76]: y = np.ones((3,))                                                                                           
In [77]: x[0]                                                                                                        
Out[77]: array([0., 0., 0.])
In [78]: np.dot(A,y)                                                                                                 
Out[78]: array([ 3., 12., 21.])
In [79]: x[0] = np.dot(A,y)                                                                                          
In [80]: x                                                                                                           
Out[80]: 
array([[ 3., 12., 21.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

您正在尝试做:

In [81]: y = np.ones((3,1))                                                                                          
In [82]: y                                                                                                           
Out[82]: 
array([[1.],
       [1.],
       [1.]])
In [83]: np.dot(A,y)                                                                                                 
Out[83]: 
array([[ 3.],
       [12.],
       [21.]])
In [84]: x[0] = np.dot(A,y)                                                                                          
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-84-48dd32783c97> in <module>
----> 1 x[0] = np.dot(A,y)

ValueError: could not broadcast input array from shape (3,1) into shape (3)
In [85]: np.mat(A)*np.mat(y)                                                                                         
Out[85]: 
matrix([[ 3.],
        [12.],
        [21.]])