I am fairly new to python and I am working with square matrices of which have size 'n' equal to a power of 2 (4x4, 8x8, etc). Instead of indexing the rows (and columns) from 0 to n-1, I want to index them using ordered pairs of two variables, so that I can extract on "sub-matrices" from the whole matrix.
For example, say I have the 4x4 matrix
M = [[1 2 3 4 ]
[5 6 7 8 ]
[9 10 11 12 ]
[13 14 15 16]]
Then I want to index the rows using the binary variables (r1,r2) so that in this case the rows should be numbered as (0,0), (0,1), (1,0) and (1,1). The columns will be indexed similarly by (c1,c2). This allows me to divide the matrix into four 2x2 "sub-matrices" based on the values of of r1 and c1.
So IDEALLY, I am expecting to get an indexing like this:
print(M[(0,0),(0,0)])
>>> 1
print(M[(1,0),(1,1)])
>>> 12
print(M[(0,1),])
>>> [5 6 7 8]
print(M[(0,),(0,)])
>>> [[1 2]
[5 6]]
Note that the variables will always not be binary valued. For eg. in an 8x8 matrix, for the pair (r1,r2), I have r1=[0,1] and r2=[0,1,2,3]. The pairing will now divide the complete matrix into four 4x4 sub-matrices.
I could do this by introducing new variables and mapping them to each value of the default indexing, but is there a more efficient way of achieving this? I feel like there has to be a built-in function in numpy or something that can do this task.