Workshop: 1 Mata Basics
Workshop: 1 Mata Basics
Workshop
This workshop contains a set of exercises that will help you to understand
the main topics covered in the lecture notes. You can get help at any time
typing
help mata
1 Mata Basics
7. C-multiply D and C, check by hand that Mata gives what you expected
8. Define w = (A :== d), make sure you understand why Mata does not
complain about conformability and exactly what w contains in each
column
10. Define comma = `˝´ and B = (Hola˝, Si˝\ No˝, Que˝). What
does C = comma:+B:+comma produce? How is this possible?
A. Miranda 2
else G = J(1,1,0)
1. Make a view of variables rep78 trunk weight length gear ratio in Mata
2. Define matrix B = (7, 4, 10\9, 2, 6) in Stata. Enter mata and make a
copy of Stata’s matrix B in Mata. Now change cell (2, 2) of Stata’s
matrix to 22 using st matrix() in Mata.
3. Define local macro xvars in Stata as displacement trunk rep78 mpg
price make. Enter Mata. Now type: vars = st local(exvars). . .What
went wrong?
4. Go back to (3). Put all elements of Stata’s local macro xvars in a Mata
matrix, where each cell should contain a single variable name. Hint:
you need to use tokens().
Introduction to Programming in Mata 3
5. While you are in Mata, change the value of variable mpg to 120 for the
first observation in Stata’s data.
4. Write a function that takes a square matrix A and returns the bottom-
right 2 × 2 corner matrix, if A is of dimension larger than 2 × 2 and A
otherwise.
5. Write a function that takes two matrices A and B and that returns an
scalar 1 if A == B entry-by-entry and 0 otherwise.
2. Create a library and add all functions you wrote in the previous section.