如何使用numpy将矩阵分割成python中的4个象限

时间:2014-09-15 19:10:55

标签: python-2.7 numpy matrix divide strassen

我是Python的新手。我试图实施Strassen的算法。在我的实现中,矩阵的大小总是2的幂。那么,我如何将矩阵划分为4个相等大小的象限?感谢

1 个答案:

答案 0 :(得分:2)

>>> 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]]

len(xs) // 2替换2。