如何使用numpy在python中将矩阵拆分为4个象限 [英] how to split matrix into 4 quadrants in python using numpy
本文介绍了如何使用numpy在python中将矩阵拆分为4个象限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的新手.我正在尝试实现Strassen的算法.在我的实现中,矩阵的大小将始终为2的幂.那么,如何将矩阵划分为4个相等大小的象限?谢谢
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
推荐答案
>>> 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.
这篇关于如何使用numpy在python中将矩阵拆分为4个象限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文