

I cannot type v*W because the 3\(\times\)1 v is not conformable with the 3\(\times\)3 W. I then create a row-wise conformable vector v, perform element-wise multiplication of v across the columns of W, and display the result. I extract the bottom four rows of W, store this matrix in W, and display this new W. Preface any matrix operator in Mata with a colon ( :) to obtain the element-by-element equivalent.ġ |. Ironically, when I am doing matrix programming, I frequently want the element-by-element operator instead of the matrix operator. In detail, closes a range-subscript extraction. I frequently extract rectangular blocks defined by a top-left element and a bottom-right element. Typing W extracts from W the rows specified in the vector u and the columns specified in the vector v. Next, I use the row-join operator, to create the row vector v and I use the column-join operator \ to create the column vector u.
Stata 12 manual generator#
I use rseed() to set the seed for the random-number generator and then use runiform( r, c ) to create a 4\(\times\)4 matrix uniform deviates, which I subsequently display. In almost all the work I do, I extract submatrices from a matrix.Įxample 2: Extracting submatrices from a matrix Type help m2_exp, marker(remarks7) for a list of operators. I then illustrate that ‘ is the transpose operator. Typing v = X*w causes Mata to assign the matrix product of X times w to v, which I subsequently display. Type help m2_op_range for details and a discussion of the row range operator. Typing w = (1::4) causes Mata to use the column range operator to create the 4\(\times\)1 column vector that was assigned to w and displayed when I typed w by itself. Type help m2_exp for details about expressions. Unassigned expressions display their results. Typing X by itself causes Mata to display what X contains, which is a 3\(\times\)4 matrix of 5s. The expression on the right-hand side of the assignment operator = is assigned to the symbol on the left-hand side. J( r, c, v ) is the Mata function that creates an r\(\times\) c matrix, each of whose elements is v.

Typing X = J(3, 4, 5) at the colon prompt causes Mata to compile and execute this code. After you type mata:, the colon prompt : is the Mata compiler asking for something to do. Typing end ends the Mata session, thereby popping back up to Stata. Typing mata: causes Stata to drop down to a Mata session. (You might find it useful to type along.)Įxample 1: A first interactive Mata session The easiest way to learn Mata is to use it.
Stata 12 manual code#
Mata code is fast because it is compiled to object code that runs on a virtual machine type help m1_how for details. Mata is a matrix programming language that is part of Stata. See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series. I recommend that you start at the beginning.

Stata 12 manual series#
This is the eleventh post in the series Programming an estimation command in Stata. I introduce Mata, the matrix programming language that is part of Stata.
