index 0 is out of bounds for axis 0 with size 0

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I am filling two arrays, field_in_k_space_REAL and field_in_k_space_IMAGINARY, with values extracted from a Gaussian distribution, paying attention to respect the symmetry to get a real field when I inverse-transform the arrays. Here is the code:

field_in_k_space_REAL = zeros(n, float) field_in_k_space_IMAGINARY = zeros(n, float)  field_in_k_space_REAL[0] = 0.0  for i in range(1, int(n/2+1)):     field_in_k_space_REAL[i] = np.random.normal(mu, math.sqrt((1/2)*math.exp(-(2*math.pi*i*sigma/L)*(2*math.pi*i*sigma/L))))  x = range(int(n/2+1), int(n)) y = range(1, int(n/2)) zipped = zip(x, y)  for j, j2 in zipped:     field_in_k_space_REAL[j] = field_in_k_space_REAL[j-2*j2]  field_in_k_space_IMAGINARY[0] = 0.0  for i in range(1, int(n/2)):     field_in_k_space_IMAGINARY[i] = np.random.normal(mu, math.sqrt((1/2)*math.exp(-(2*math.pi*i*sigma/L)*(2*math.pi*i*sigma/L))))  field_in_k_space_IMAGINARY[n/2] = 0.0  for j, j2 in zipped:     field_in_k_space_IMAGINARY[j] = - field_in_k_space_IMAGINARY[j-2*j2]  print 'field_k', field_in_k_space_REAL 

But I keep having the following error:

 field_in_k_space_REAL[0] = 0.0 IndexError: index 0 is out of bounds for axis 0 with size 0 

Can someone explain why and how to fix it?

回答1:

My guess is that the array field_in_k_space_REAL is actually of length 0, most likely because you set n = 0 further up in your code (do you use n in a loop maybe?). I can reproduce the error when I directly initialize an array of length 0.



回答2:

in fact, you'd better use:

field_in_k_space_REAL.loc[index] = 0.0 

instead of:

field_in_k_space_REAL[index] = 0.0 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!