用另一个数组替换数组的特定列:错误原因

时间:2019-04-21 23:37:10

标签: arrays python-2.7 numpy

我正在尝试求解一组线性方程,这些线性方程是递归求解的。在每个时间步骤中,我的解决方案都是gamma,其形状为(3,1)。该系统被迭代求解20次,以达到gamma的最终值。

我试图每次将gamma的值存储在另一个数组中,以便在代码运行完成后的每一步都可以访问gamma的值。当我尝试将每一步之后的gamma值存储到gamma_solution中时,会出现以下错误:

SyntaxError: can't assign to function call

我要去哪里错了?有更好的方法吗?

谢谢

输入代码:

gamma_solution = np.zeros((3,#_of_steps))

for i in range(#_of_steps):
    <code to solve a system of equations to give gamma as result>
    gamma_solution[:,i].reshape((3,1)) = gamma

输出:

Error

期望值:在第i步的每个步骤中,将在该步骤中获得的gamma值存储在gamma_solution的第i列中

1 个答案:

答案 0 :(得分:0)

好的,gamma_solution的形状是3xN,gamma_solution[:, j] shape is (3,), so you need to transpose gamma (that has shape (3, 1)) to store it in j-th column of gamma_solution`。请参见下面的代码:

import numpy as np
N = 10
gamma_solution = np.zeros((3, N))
gamma = np.arange(3)[:, np.newaxis]
for j in range(N):  # main loop where gamma values are computed
    gamma_solution[:, j] = gamma.T
相关问题