0% found this document useful (0 votes)
59 views3 pages

Workshop: 1 Mata Basics

This document provides exercises to practice Mata programming skills. It covers defining matrices, performing operations like joining matrices and extracting submatrices, using loops and conditional statements, interacting with Stata from Mata, and writing custom functions. The exercises demonstrate important Mata concepts like defining matrices, performing operations on matrices, using control flow, interacting with Stata data and macros from Mata, and writing reusable functions.

Uploaded by

lahgrita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

Workshop: 1 Mata Basics

This document provides exercises to practice Mata programming skills. It covers defining matrices, performing operations like joining matrices and extracting submatrices, using loops and conditional statements, interacting with Stata from Mata, and writing custom functions. The exercises demonstrate important Mata concepts like defining matrices, performing operations on matrices, using control flow, interacting with Stata data and macros from Mata, and writing reusable functions.

Uploaded by

lahgrita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Programming in Mata 1

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

either in the Stata or the Mata prompt.

1 Mata Basics

Define A = (1, 2, 3\4, 5, 6), B = (7, 8\9, 10), and C = (15\16\17)

1. Get A0 A and put the result in scalar c

2. Join A and C 0 to get a 3 × 3 matrix D

3. Extract the first row of matrix D and put it in a vector d

4. Change to 35 cell (2, 3) of D using an assignment

5. Extract the top 2 × 2 sub-matrix of D using range subscripts

6. Extract the second column of D using list subscripting

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

9. Why does A :∧ B produce a error message?

10. Define comma = `˝´ and B = (Hola˝, Si˝\ No˝, Que˝). What
does C = comma:+B:+comma produce? How is this possible?
A. Miranda 2

2 Loops and flow control


Define A = (1, 2, 3\4, 5, 6), B = (7, 4, 10\9, 2, 6), and C = (15\16\17)

1. Expand, writing explicitly the braces, the following expression:


if (w[1,2]==14) G = G:*H

else if (w[5,6]==11) G= G:*R

else G = J(1,1,0)

2. Write a for loop that changes A[i, j] to 15 if A[i, j] = B[i, j]


3. Mata runiform(r,c) function returns an r × c real matrix containing
uniformly distributed random variates on [0, 1). Write a do...while
loop that stops when runiform(1,1) returns a value that is larger than
0.75.
4. Modify your programme in (3) to exit the do...while loop if the pro-
cess has taken more than 5 iteration steps.
5. Modify your programme in (3) to disregard the step if runiform(1,1)
returns a value larger than 0.75 the first time the loop is run.

3 Interacting with Stata


Use Stata ’s 1978 Automobile Data:
sysuse auto, clear

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 Writing new functions in Mata


1. Write a function that takes nothing as argument and returns “Hello
world”.

2. Write a function that takes a matrix as unique argument and returns


it squared.

3. Write a function that takes two conformable square matrices A and B


and that exchanges A’s diagonal by B 0 s diagonal.

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.

5 Displaying output & saving Mata functions


1. Save all the new functions you wrote in the previous section in their
corresponding mo-files.

2. Create a library and add all functions you wrote in the previous section.

You might also like