help matrix define, help matrix input dialog: matrix define matrix input
-------------------------------------------------------------------------------
Title
[P] matrix define -- Matrix definition
Syntax
Perform matrix computations
matrix [define] A = matrix_expression
Input matrices
matrix [input] A = (#[,#...] [\ #[,#...] [\ [...]]])
Menu
matrix define
Data > Matrices, ado language > Define matrix from expression
matrix input
Data > Matrices, ado language > Input matrix by hand
Description
matrix define performs matrix computations. The word define may be
omitted.
matrix input provides a method for inputting matrices. The word input
may be omitted (see the discussion that follows).
See [P] matrix for background information and links to more matrix help.
See [M-2] exp for matrix expressions in Mata.
Remarks
matrix define calculates matrix results from other matrices. For
instance,
. matrix define D = A + B + C
creates D containing the sum of A, B, and C. The word define may be
omitted,
. matrix D = A + B + C
and the command may be further abbreviated:
. mat D = A + B + C
The same matrix may appear on both the left and the right of the equals
sign in all contexts, and Stata will not become confused. Complicated
matrix expressions are allowed; see matrix operators and matrix
functions.
With matrix input, you define the matrix elements rowwise; commas are
used to separate elements within a row, and backslashes are used to
separate the rows. Spacing does not matter.
. matrix input A = (1,2\3,4)
The above would also work if you omitted the input subcommand:
. matrix A = (1,2\3,4)
There is a subtle difference: the first method uses the matrix input
command, and the second uses the matrix expression parser. Omitting
input allows expressions in the command. For instance,
. matrix X = (1+1, 2*3/4 \ 5/2, 3)
is understood but
. matrix input X = (1+1, 2*3/4 \ 5/2, 3)
would produce an error.
matrix input, however, has two other advantages. First, it allows input
of large matrices. (The expression parser is limited because it must
"compile" the expression and, if it is too long, will produce an error.)
Second, matrix input allows you to omit the commas.
Examples
Create 2 x 2 matrix mymat
. matrix input mymat = (1,2\ 3, 4)
List the contents of mymat
. mat list mymat
Drop matrix mymat
. mat drop mymat
Create 2 x 2 matrix mymat
. matrix mymat = (1,2\ 3, 4)
Create 1 x 4 row vector myrvec
. matrix input myrvec = (1.7, 2.93, -5, 3)
List the contents of myrvec
. mat list myrvec
Create 4 x 1 column vector mycvec
. matrix mycvec = (1.7 \ 2.93 \ -5 \ 3)
List the contents of mycvec
. mat list mycvec
Also see
Manual: [P] matrix define
Help: [P] matrix; [U] 13 Functions and expressions (expressions), [D]
functions (matrix functions), matrix operators