Exercise
Repeat the additional exercise from the matrices chapter and pay attention to the differences.
-
Create in Mata
- a scalar a that contains the number 2,
- a matrix A with 4 rows and 4 columns and all the elements being 2, and
- a matrix B of the same dimension, which has the value 2 on the diagonal and 0 otherwise.
-
Do the following computations in Mata:
- Multiply the scalar a with matrix A and store the result in matrix C
- Calculate the sum of matrix C and the inverse of matrix B and store it in matrix D
- Combine matrices A, B, C and D into one 8x8 matrix E
- Create a vector b that contains the first, third and fifth element of the first row of E
Implement the following exercise using as many Mata shortcuts as possible.
-
Add 20 rows to matrix A with the following values:
- In column 1, start with the value 1 in the first new row and increase in increments of 1 to the value 20, the second column starts with the value 2 and increases in increments of 1 to the value 21, the third column starts with the value 3 and increases in increments of 1 to the value 22 and the fourth column starts with the value 4 and increases in increments of 1 to the value 23.
- Display the entire 12th row
- Display the second to fourth column in the last five rows
- Create a selection vector s, which has as many rows as A, the value 1 in rows 2, 12 and 14 and zeros otherwise
- Display all rows of matrix A, for which vector s is 1
- Display all columns of matrix A, for which the value in row 7 is smaller than 5