Good afternoon,
I would like to form a block diagonal matrix which has square blocks of 1s on the diagonal, and the dimensions of these blocks are given in a Stata-vector. E.g.,
Now I want to form in Mata using blockdiag() a square block diagonal matrix of dimension 69 X 69, which has starting from the north-west corner of the matrix and going down to the south-east corner blocks of 1s on the main diagonal of dimenstions 2 X 2, 8 X 8, 30 X 30, 18 X 18, and 11 X 11.
If it is of any help, this is how I do this in Stata:
I would like to form a block diagonal matrix which has square blocks of 1s on the diagonal, and the dimensions of these blocks are given in a Stata-vector. E.g.,
Code:
. sysuse auto, clear
(1978 automobile data)
. levelsof rep, matcell(Dimensions)
1 2 3 4 5
. mat list Dimensions
Dimensions[5,1]
c1
r1 2
r2 8
r3 30
r4 18
r5 11
. return list
scalars:
r(N) = 69
r(r) = 5
macros:
r(levels) : "1 2 3 4 5"
If it is of any help, this is how I do this in Stata:
Code:
. mat Blocks = J(r(N),r(N),0)
. scalar J = 1
. forvalues l = 1/`r(r)' {
2. mat Blocks[J,J] = J(Dimensions[`l',1], Dimensions[`l',1], 1)
3. scalar J = J + Dimensions[`l',1]
4. }

Comment