Let's say I have a square matrix of size (n*m) x (n*m) that is composed of n x n sub-matrices, with each submatrix being a square of size m x m.
I want to select the diagonal indices of the off-diagonal submatrices. This is the code that I have so far, but I would like to do this without the calls to itertools.permutations
if possible.
n_submatrix = 3
submatrix_size = 2
my_matrix = np.zeros([n_submatrix * submatrix_size,
n_submatrix * submatrix_size])
for submatrix_index in itertools.permutations(range(n_submatrix),2):
my_slice0 = slice(submatrix_index[0]*submatrix_size, (submatrix_index[0]+1)*submatrix_size)
my_slice1 = slice(submatrix_index[1]*submatrix_size, (submatrix_index[1]+1)*submatrix_size)
np.fill_diagonal(my_matrix[my_slice0,my_slice1],1)
my_matrix
#array([[ 0., 0., 1., 0., 1., 0.],
# [ 0., 0., 0., 1., 0., 1.],
# [ 1., 0., 0., 0., 1., 0.],
# [ 0., 1., 0., 0., 0., 1.],
# [ 1., 0., 1., 0., 0., 0.],
# [ 0., 1., 0., 1., 0., 0.]])