Stata Matrix Slicing, Select Subset of Matrix Values, Subset of Rows and Columns (DO, more see: Fan and Stata4Econ)

-----------------------------------------------------------------------------------------------------------------------------
      name:  matrix_select_subset
       log:  C:\Users\fan/Stata4Econ//matrix/define/basic.smcl
  log type:  smcl
 opened on:   6 Oct 2019, 18:49:17


. log on $st_logname (log already on)

. . ///-- Site Link: Fan's Project Reusable Stata Codes Table of Content > di "https://fanwangecon.github.io/" https://fanwangecon.github.io/

. di "https://fanwangecon.github.io/Stata4Econ/" https://fanwangecon.github.io/Stata4Econ/

. . ///-- File Title > global filetitle "Stata Matrix Slicing, Select Subset of Matrix Values, Subset of Rows and Columns"

. . . ///--- Generate matrix with all 0 > scalar it_rowcnt = 4

. scalar it_colcnt = 6

. scalar bl_fillval = 0

. matrix mt_bl_estd = J(it_rowcnt, it_colcnt, bl_fillval)

. . ///--- Give Matrix Row and Column Names > matrix rownames mt_bl_estd = hhfe vilfe provfe morecontrols

. matrix colnames mt_bl_estd = reg1 reg2 reg3 reg4 reg5 reg6

. . ///--- Assign value to matrix cell single > matrix mt_bl_estd[rownumb(mt_bl_estd, "hhfe"), colnumb(mt_bl_estd, "reg1")] = 1

. matrix mt_bl_estd[2,2] = 3

. . ///--- Assign value to 4th row, 3nd to 6th > matrix mt_bl_estd[4,3] = (9,8,7,6)

. . ///--- Assign value to 4th column, 2nd 3rd values > matrix mt_bl_estd[2,4] = (-3\-44.3)

. . ///--- Obtain value from matrix > scalar bl_hhfe_reg1 = mt_bl_estd[rownumb(mt_bl_estd, "hhfe"), colnumb(mt_bl_estd, "reg1")]

. di bl_hhfe_reg1 1

. di el(mt_bl_estd, rownumb(mt_bl_estd, "hhfe"), colnumb(mt_bl_estd, "reg1")) 1

. . ///--- Select a column from matrix > matrix mt_bl_estd_colreg1 = mt_bl_estd[1..., colnumb(mt_bl_estd, "reg1")]

. matrix list mt_bl_estd_colreg1

mt_bl_estd_colreg1[4,1] reg1 hhfe 1 vilfe 0 provfe 0 morecontrols 0

. . ///--- Get Row and Column Names > global st_colnames : colnames mt_bl_estd

. di "${st_colnames}" reg1 reg2 reg3 reg4 reg5 reg6

. global st_rownames : rownames mt_bl_estd

. di "${st_rownames}" hhfe vilfe provfe morecontrols

. . ///--- Show Matrix > matrix list mt_bl_estd

mt_bl_estd[4,6] reg1 reg2 reg3 reg4 reg5 reg6 hhfe 1 0 0 0 0 0 vilfe 0 3 0 -3 0 0 provfe 0 0 0 -44.3 0 0 morecontrols 0 0 9 8 7 6

. . ///--- End Log and to HTML > log close _all name: matrix_select_subset log: C:\Users\fan/Stata4Econ//matrix/define/basic.smcl log type: smcl closed on: 6 Oct 2019, 18:49:17 -----------------------------------------------------------------------------------------------------------------------------