Numpy无法播放阵列

时间:2016-01-29 13:51:39

标签: arrays python-3.x numpy anaconda

我正在编写代码来模仿康威的生命游戏(10细胞系)。我差不多完成但是我的代码给了我错误

could not broadcast array from shape (7,11) into shape (10)

以下是我认为存在问题的代码。

glider = np.zeros([7,11])
glider[3,:]=1
glider[(3,0)]=0
glider[(3,10)]=0
N=(10) # the domain is NxN, N=10 for testing, more interesting with N=100
init_choice = 1 # 0 for random initialization
c = np.random.choice( (1,0), N*N, p=[0.3,0.7] ).reshape(N,N) # default initialization is random
if init_choice == 1:
    c = c*0
    c[3,:] = glider # put glider in top left
if init_choice == 2:
    print("not implemented yet")

periodic(c)

给出结果

-----------------------------------------------------------------------------------
ValueError                                        Traceback (most recent call last)
<ipython-input-53-0a8812bfb2f8> in <module>()
      4 if init_choice == 1:
      5     c = c*0
      6     c[3,:] = glider # put glider in top left
      7 if init_choice == 2:
      8     print("not implemented yet")

ValueError: could not broadcast input array from shape (7,11) into shape (10)

这是screenshot

1 个答案:

答案 0 :(得分:0)

cglider的形状不匹配。以交互方式尝试。一个是10x10,另一个是7x11。

在1d中将一个数组复制到另一个数组(更大)涉及如下内容:

A = np.zeros(10)
B = np.ones(2)
A[3:5] = B