= Stata Matrices = '''Matrices''' are a data type used for meta programming in the Stata programming language. See also [[Stata/Scalars|scalars]]. <> ---- == Definition == To define a matrix, use either the `matrix` or `matrix input` command. The latter only allows literals in the definition, while the former allows expressions like `1/3` or `_pi`. The former also allows assignment by matrix arithmetic. The matrix's values are bounded by parentheses (`(` and `)`). Values in a row are delimited with a comma (`,`). Rows are delimited with a backslash (`\`). {{{ . matrix input A = (1,2\3,4) . matrix list A A[2,2] c1 c2 r1 1 2 r2 3 4 }}} Matrices can be defined with other matrices as well. {{{ . matrix B = (A,A) . matrix list B B[2,4] c1 c2 c1 c2 r1 1 2 1 2 r2 3 4 3 4 . matrix C = (A\A) . matrix list C C[4,2] c1 c2 r1 1 2 r2 3 4 r1 1 2 r2 3 4 }}} A '''vector''' is simply a special case of a matrix. === Rows and Columns === A newly-defined matrix's rows and columns have sequential names (`r1`, `r2`, and so on). Many commands return matrices with names rows and columns. Many more commands are able to parse passed matrices ''only'' because rows and columns are named in a structured way. ---- == Usage == Matrixes can be joined in either direction. {{{ . matrix A = (1) . matrix B = A , (2) . matrix list B B[1,2] c1 c2 r1 1 2 . matrix C = B \ (3,4) . matrix list C C[2,2] c1 c2 r1 1 2 r2 3 4 }}} A matrix is indexed by `NAME[ROW,COL]`. Indexing starts at 1. `C[1,2]` is 2 in the above example. === Arithmetic === Addition (and subtraction) is defined for any like-sized matrices. Multiplication is defined for any ''a x b'' and ''b x c'' matrices, the result being ''a x c''. The transpose of matrix `A` is defined with `A'`. The negation of matrix `A` is defined with `-A`. Division by [[Stata/Scalars|scalar]] is performed with the forward slash (`/`) operator. The Kronecker product is performed with the hash (`#`) operator. === Functions === Matrices support these functions. ||'''Function''' || ||`cholesky(M)` || ||`corr(M)` || ||`diag(v)` || ||`get(systemname)` || ||`hadamard(M,N)` || ||`I(n)` || ||`inv(M)` || ||`invsym(M)` || ||`J(r,c,z)` || ||`matuniform(r,c)` || ||`nullmat(matname)`|| ||`sweep(M,i)` || ||`vec(M)` || ||`vecdiag(M)` || Matrices also support these functions which return a scalar. ||'''Function''' || ||`coleqnumb(M,s)` || ||`colnfreeparms(M)`|| ||`colnumb(M,s)` || ||`colsof(M)` || ||`det(M)` || ||`diag0cnt(M)` || ||`el(M,i,j)` || ||`issymmetric(M)` || ||`matmissing(M)` || ||`mreldif(X,Y)` || ||`roweqnumb(M,s)` || ||`rownfreeparms(M)`|| ||`rownumb(M,s)` || ||`rowsof(M)` || ||`trace(M)` || ---- == See also == [[https://www.stata.com/manuals/u14.pdf|Stata manual section 14: Matrix expressions]] ---- CategoryRicottone