I want to divide a 4x4 array into a list of list of four 2x2 array. This
| 0 1 2 3 |
| 4 5 6 7 |
| 8 9 10 11 |
|12 13 14 15 |
should be divided into four blocks as
| |0 1| |2 3 | |
| |4 5| |6 7 | |
| |
| | 8 9| |10 11| |
| |12 13| |14 15| |
so, if I access block 1 then it should be [2,3],[6,7].
I wrote this method:
from numpy import *
from operator import itemgetter
def divide_in_blocks(A):
A1 = hsplit(A, A[0].size/2)
for i, x in enumerate(A1):
A1[i] = vsplit(x, x.size/4)
return A1
if __name__ == '__main__':
a = arange(16).reshape(4,4)
a1 = divide_in_blocks(a)
#print a
#a1 = sorted(a1, key=itemgetter(2))
print a1
which divides the array as
| |0 1| | 8 9 | |
| |4 5| |12 13 | |
| |
| |2 3| |10 11| |
| |6 7| |14 15| |
i.e. I am getting block 1 as [8, 9], [12, 13].
Output of the code:
[[array([[0, 1],
[4, 5]]),
array([[ 8, 9],
[12, 13]])],
[array([[2, 3],
[6, 7]]),
array([[10, 11],
[14, 15]])]]
Is there any way to sort this list of list of arrays to get the desired output:
[[array([[0, 1],
[4, 5]]),
array([[2, 3],
[6, 7]])],
[array([[ 8, 9],
[12, 13]]),
array([[10, 11],
[14, 15]])]]
?