Differences between revisions 2 and 4 (spanning 2 versions)
Revision 2 as of 2023-06-09 13:30:32
Size: 2157
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.
Line 82: Line 39:
Addition (and subtraction) is defined for any like-sized matrices. Matrix elements can be accessed by subscripting (row then column) while indexing from 1. Following from the above example, `A[1,2]` returns 2.
Line 84: Line 41:
Multiplication is defined for any ''a x b'' and ''b x c'' matrices, the result being ''a x c''. To instead show the values of an entire matrix, try `matrix list A`.
Line 86: Line 43:
The transpose of matrix `A` is defined with `A'`. To join matrices horizontally, try
Line 88: Line 45:
The negation of matrix `A` is defined with `-A`. {{{
matrix C = (A, B)
}}}
Line 90: Line 49:
Division by [[Stata/Scalars|scalar]] is performed with the forward slash (`/`) operator. To join matrices vertically, try
Line 92: Line 51:
The Kronecker product is performed with the hash (`#`) operator. {{{
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 [[LinearAlgebra/Transposition|transposition]], as in `A'`

----



== See also ==

[[https://www.stata.com/manuals/u14.pdf|Stata manual section 14: Matrix expressions]]

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:


See also

Stata manual section 14: Matrix expressions


CategoryRicottone

Stata/Matrices (last edited 2025-10-24 15:47:45 by DominicRicottone)