索引1超出轴0的范围,矩阵中的大小为1

时间:2017-07-04 17:38:20

标签: python numpy

所以他的代码用于3xN维矩阵,其中过去现在和将来是3行。我已将其更改为一维数组以便更快地进行计算。我哪里错了

psi_past_real = np.zeros((1,domain)) #  real part 3xN matrix 
psi_past_imaginary = np.zeros((1,domain))
psi_present_real = np.zeros((1,domain))
psi_present_imaginary = np.zeros((1,domain)) 
psi_future_real = np.zeros((1,domain))
psi_future_imaginary = np.zeros((1,domain))
psi_probability = np.zeros(domain,)   # Probability


xn = range(1,domain/2)
x = X[xn]/dx    #  Normalized position coordinate
gg = Gaussian(x,x0,sigma)
cx = np.cos(k0*x)
sx = np.sin(k0*x)
psi_present_real[xn] = cx*gg
psi_present_imaginary[xn] = sx*gg
psi_past_real[xn] = cx*gg
psi_past_imaginary[xn] = sx*gg

高斯函数是

def Gaussian(x,t,sigma):
    np.exp(-(x-t)**2/(2*sigma**2))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Applications/Spyder 2.7.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 586, in runfile
    execfile(filename, namespace)
  File "/Users/kabirthakur/Desktop/untitled1.py", line 73, in <module>
    psi_present_real[xn] = cx*gg
IndexError: index 1 is out of bounds for axis 0 with size 1

1 个答案:

答案 0 :(得分:1)

psi_present_real的形状是(1,domain)。如果要按第二个坐标对其进行索引,则需要在第一个坐标中使用冒号来表示:

psi_present_real[:, xn] = cx*gg