0
votes
  1. To create a matrix M= (N*50) matrix m=J(N,50,.)

    Since I want to change the number of observation overtime, how to I set N=number of observations?

  2. How to specific each element of the matrix, e.g. M(1,2) (element in row1, column2)?

  3. How to I assign value to each element of the matrix? For example I want to set O to the first row to be all 0, i.e. M(1,1)=M(1,2)=...M(1,50)=0?

2

2 Answers

1
votes
. // an (empty) dataset with 5 observations
. clear all

. set obs 5
obs was 0, now 5

. 
. // go to Mata
. mata:
------------------------------------------------- mata (type end to exit) -------------------------
: 
: // collect the number of observations
: n = st_nobs()

: 
: // create matrix M (n x 10)
: M = J(n, 10, .)

: 
: // see it:
: M
        1    2    3    4    5    6    7    8    9   10
    +---------------------------------------------------+
  1 |   .    .    .    .    .    .    .    .    .    .  |
  2 |   .    .    .    .    .    .    .    .    .    .  |
  3 |   .    .    .    .    .    .    .    .    .    .  |
  4 |   .    .    .    .    .    .    .    .    .    .  |
  5 |   .    .    .    .    .    .    .    .    .    .  |
    +---------------------------------------------------+

: 
: // fill the first row with 0s
: M[1,.] = J(1,10,0)

: 
: // See cell 1,2
: M[1,2]
  0

: 
: // see cell 2,3
: M[2,3]
  .

: end
0
votes

Creating a .do file.

Just use any text editor, including Stata's do-file editor, and save with the extension .do.

The same random normal deviate in a row. Here is one way:

: m = J(5,5, .)

: m[1,] = J(1, 5, rnormal(1,1,0,1))

: m
                 1             2             3             4             5
    +-----------------------------------------------------------------------+
  1 |  2.285713101   2.285713101   2.285713101   2.285713101   2.285713101  |
  2 |            .             .             .             .             .  |
  3 |            .             .             .             .             .  |
  4 |            .             .             .             .             .  |
  5 |            .             .             .             .             .  |
    +-----------------------------------------------------------------------+