0
votes

I want to use Mata to create a block diagonal matrix. There is an example in the documentation for three matrices:

blockdiag(Z1, blockdiag(Z2,Z3))

It's not clear to me if that can be extended to more matrices:

blockdiag(Z1, blockdiag(Z2, blockdiag(Z3,Z4)))
blockdiag(Z1, blockdiag(z2, blockdiag(Z3, blockdiag(Z4,Z5))))

or if some looping structure could be written to more efficiently complete the matrix. Suggestions/insight appreciated.

1

1 Answers

2
votes

All you need to do is try it out. That's simply nesting blockdiag(), and it works:

mata:

A = (1,2 \ 3,4)
B = (5,6 \ 7,8)
C = (9,10 \ 11,12)
D = (13,14 \ 15,16)

AB = blockdiag(A,B)

ABC = blockdiag(A, blockdiag(B,C))

ABCD = blockdiag(A, blockdiag(B, blockdiag(C,D)))

AB

ABC

ABCD

end