实例:Create a 8x8 matrix and fill it with a checkerboard pattern(创建一个8x8的棋盘式矩阵)
import numpy as np
z = np.zeros((8,8))
z[1::2,::2] = 1
[[0 0 0 0 0 0 0 0]
[1 0 1 0 1 0 1 0]
[0 0 0 0 0 0 0 0]
[1 0 1 0 1 0 1 0]
[0 0 0 0 0 0 0 0]
[1 0 1 0 1 0 1 0]
[0 0 0 0 0 0 0 0]
[1 0 1 0 1 0 1 0]]
上面实现的是矩阵中从第‘1’行开始每隔一行,从第‘0’列开始,每隔1列 赋值为1
import numpy as np
z = np.zeros((8,8))
z[::2,1::2] = 1
[[0 1 0 1 0 1 0 1]
[0 0 0 0 0 0 0 0]
[0 1 0 1 0 1 0 1]
[0 0 0 0 0 0 0 0]
[0 1 0 1 0 1 0 1]
[0 0 0 0 0 0 0 0]
[0 1 0 1 0 1 0 1]
[0 0 0 0 0 0 0 0]]
上面实现的是矩阵中从第‘0’行开始每隔1行,从第‘1’列开始,每隔1列 赋值为1
将上面两个操作合起来,我们将得到一个棋盘式的8x8矩阵:
import numpy as np
z = np.zeros((8,8))
z[1::2,::2] &#