|
Size: 3176
Comment:
|
← Revision 4 as of 2025-10-24 15:47:45 ⇥
Size: 1834
Comment: Rewrite
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 3: | Line 3: |
| '''Matrices''' are a data type used for meta programming in the Stata programming language. See also [[Stata/Scalars|scalars]]. | '''Matrices''' are a fundamental data type in Stata, like [[Stata/Scalars|scalars]]. Note that [[Stata/Mata|Mata]] does not use this data type. |
| Line 11: | Line 13: |
| == Definition == | == Description == |
| Line 13: | Line 15: |
| 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. | Matrices are collections of numeric values. |
| Line 15: | Line 17: |
| The matrix's values are bounded by parentheses (`(` and `)`). Values in a row are delimited with a comma (`,`). Rows are delimited with a backslash (`\`). | Many commands return matrices; for example: |
| Line 18: | Line 20: |
| . matrix input A = (1,2\3,4) . matrix list A A[2,2] c1 c2 r1 1 2 r2 3 4 |
correlate foo bar baz, covariance matrix cov = r(C) |
| Line 25: | Line 23: |
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. |
|
| Line 62: | Line 30: |
| Matrixes can be joined in either direction. | To declare a matrix, try: |
| Line 65: | Line 33: |
| . 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 |
matrix A = (1, 2 \ 3, 4) matrix input B = (5, 6 \ 1/3, _pi) |
| Line 80: | Line 37: |
| A matrix is indexed by `NAME[ROW,COL]`. Indexing starts at 1. `C[1,2]` is 2 in the above example. | The '''`matrix input`''' subcommand allows for expressions as seen above, but otherwise does not differ. Matrix elements can be accessed by subscripting (row then column) while indexing from 1. Following from the above example, `A[1,2]` returns 2. To instead show the values of an entire matrix, try `matrix list A`. To join matrices horizontally, try {{{ matrix C = (A, B) }}} To join matrices vertically, try {{{ matrix C = (A \ B) }}} A '''vector''' is simply a special case of a matrix. |
| Line 84: | Line 59: |
| === Arithmetic === | === Row and Column Names === |
| Line 86: | Line 61: |
| Addition (and subtraction) is defined for any like-sized matrices. | A new matrix uses sequential names for rows and columns (i.e., `r1`, `c1`, and so on). |
| Line 88: | Line 63: |
| Multiplication is defined for any ''a x b'' and ''b x c'' matrices, the result being ''a x c''. | Commands that return matrices usually name the rows and columns. Commands that consume matrices often assume a common structure, and therefore require that specific names be used. |
| Line 90: | Line 65: |
| The transpose of matrix `A` is defined with `A'`. | To assign names, try: |
| Line 92: | Line 67: |
| 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. |
{{{ matrix rownames A = foo bar baz matrix colnames A = ham spam eggs }}} |
| Line 100: | Line 74: |
| === Functions === | === Operators === |
| Line 102: | Line 76: |
| Matrices support these functions. | Most operators work as expected. Note that the slash (`/`) performs element-wise scalar division. |
| Line 104: | Line 78: |
| ||'''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)` || |
New operators include: * apostrophe (`'`) for [[LinearAlgebra/Transposition|transposition]], as in `A'` |
Stata Matrices
Matrices are a fundamental data type in Stata, like scalars.
Note that Mata does not use this data type.
Description
Matrices are collections of numeric values.
Many commands return matrices; for example:
correlate foo bar baz, covariance matrix cov = r(C)
Usage
To declare a matrix, try:
matrix A = (1, 2 \ 3, 4) matrix input B = (5, 6 \ 1/3, _pi)
The matrix input subcommand allows for expressions as seen above, but otherwise does not differ.
Matrix elements can be accessed by subscripting (row then column) while indexing from 1. Following from the above example, A[1,2] returns 2.
To instead show the values of an entire matrix, try matrix list A.
To join matrices horizontally, try
matrix C = (A, B)
To join matrices vertically, try
matrix C = (A \ B)
A vector is simply a special case of a matrix.
Row and Column Names
A new matrix uses sequential names for rows and columns (i.e., r1, c1, and so on).
Commands that return matrices usually name the rows and columns. Commands that consume matrices often assume a common structure, and therefore require that specific names be used.
To assign names, try:
matrix rownames A = foo bar baz matrix colnames A = ham spam eggs
Operators
Most operators work as expected. Note that the slash (/) performs element-wise scalar division.
New operators include:
apostrophe (') for transposition, as in A'
See also
Stata manual section 14: Matrix expressions
