Efficient way to create a block diagonal?

I have a matrix A of dim(3,3) . Using torch.block_diag(A,A,…A) I can create a block diagonal which has 200 blocks of A on the diagonals but this code is very inefficient as I have to carefully type A 200 times . Please can I know if there is an efficient way to achieve this?

2 Likes

You can achieve this with repeat before calling block_diag:

A_200 = A.unsqueeze(0).repeat(200,1,1)
torch.block_diag(*A_200)
3 Likes

Thank you :slight_smile: