我是新来的Python。 我想实现的Strassen的算法。 矩阵的大小将始终是2在我的执行权力。 所以,我怎么划分的矩阵为4个大小相等的象限? 谢谢
Answer 1:
>>> xs = np.arange(16)
>>> xs
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> xs.reshape(4, 4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> xs = xs.reshape(4, 4)
>>> a, b, c, d = xs[:2, :2], xs[2:, :2], xs[:2, 2:], xs[2:, 2:]
>>> print(a, b, c, d, sep='\n')
[[0 1]
[4 5]]
[[ 8 9]
[12 13]]
[[2 3]
[6 7]]
[[10 11]
[14 15]]
代替2,用len(xs) // 2
。
文章来源: how to split matrix into 4 quadrants in python using numpy