Arrays Numpy无法广播数组
我正在写代码来模仿康威的生命游戏(10细胞系)。我几乎完成了,但是我的代码给了我错误Arrays Numpy无法广播数组,arrays,python-3.x,numpy,anaconda,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 interesti
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)
-----------------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
4如果init_choice==1:
5c=c*0
6c[3,:]=滑翔机#将滑翔机放在左上角
7如果init_choice==2:
8打印(“尚未实施”)
ValueError:无法将输入数组从形状(7,11)广播到形状(10)
这是一个。c和滑翔机的形状不匹配。以交互方式尝试。一个是10x10,另一个是7x11 在1d中,将一个阵列复制到另一个(更大)阵列涉及以下内容:
A = np.zeros(10)
B = np.ones(2)
A[3:5] = B
我想我不知道在哪里更改CTS的大小。这是一个基本的numpy索引问题,您需要自己解决。如何将任何形状的滑翔机放入
c
中相同形状的单元格子集中。小型启动-1个电池、2个、2x2等。