how to split matrix into 4 quadrants in python using numpy

六眼飞鱼酱① 提交于 2019-12-11 09:43:56

问题


I'm new to Python. I'm trying to implement Strassen's Algorithm. The size of the matrix will always be a power of 2 in my implementation. So, how do I divide the matrix into 4 equal sized quadrants? Thanks


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

replace 2, with len(xs) // 2.



来源:https://stackoverflow.com/questions/25855180/how-to-split-matrix-into-4-quadrants-in-python-using-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!