Reputation: 887
I generated a 10x10 matrix, and a 10-observation variable (its values don't matter).
I want to insert all the variable values into the first column of the matrix.
I'm struggling with "subsetting" both the variable and the matrix.
matrix M = J(10, 10, .)
egen V = seq(), f(1) t(10)
matrix M[1:_N, 1] = V[1:_N]
Upvotes: 1
Views: 1803
Reputation: 3261
If the expression on the right evaluates to a matrix (not a scalar), this will replace the submatrix with the top left element given, so only the the top left element on the left side of the equation will be enough.
As for the variable subscripting, I'm afraid you can refer to one observation only. You will need an extra step to put data from a variable into a matrix, using mkmat
.
clear
set obs 10
matrix M = J(10, 10, .)
egen V = seq(), f(1) t(10)
mkmat V in 1/10 // Put observations 1 to 10 from variable V into matrix V
matrix M[1,1] = V // Replace submatrix of M with top left element 1,1 with V
Upvotes: 3