0% found this document useful (0 votes)
102 views

Exercise 2 - Solution

This document contains an exercise with 6 questions about matrices in R. The questions involve constructing matrices with specific values, extracting elements and rows from matrices, performing operations like addition and multiplication on matrices, and combining vectors to form a matrix. Suggested solutions with R code are provided for each question to demonstrate how to work with matrices programmatically.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Exercise 2 - Solution

This document contains an exercise with 6 questions about matrices in R. The questions involve constructing matrices with specific values, extracting elements and rows from matrices, performing operations like addition and multiplication on matrices, and combining vectors to form a matrix. Suggested solutions with R code are provided for each question to demonstrate how to work with matrices programmatically.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EIA1013

PROGRAMMING TOOLS FOR ECONOMICS

EXERCISE 2
MATRICES

QUESTIONS

1. Construct a 4 × 4 blank matrix A. Then fill the matrix column-wise with the values
from 1 to 16.

2. Construct a matrix B and define the column and row names as below:

col1 col2 col3 col4


row1 1 2 3 4
row2 5 6 7 8
row3 9 10 11 12
row4 13 14 15 16

3. Construct and store a 4 × 4 matrix F that is filled row-wise with values from 1 to 16.
Then extract out:
a) The element at the third column and second row.
b) Third row.
c) Forth column.

1 3 5 0 2 0
4. Let 𝑚1 = [ ] and 𝑚2 = [ ]. Add and subtract the matrixes. Then,
2 4 6 1 3 2
multiply matrix 𝑚1 with transpose matrix 𝑚2. Finally, estimate the inverse of the
output.

4
5. Given 𝑟1 = [1 1 2], 𝑟2 = [2 1 2], 𝑐1 = [ ]. Create a matrix G from these
4
vectors as below:

[,1] [,2] [,3] [,4]


[1,] 1 1 2 4
[2,] 1 1 2 4

6. For
1 2 3
𝐻 = [4 5 6]
7 8 9
Extract the rows that have a value > 5.
SUGGESTED SOLUTIONS

Q1
> A<-matrix(,nrow=4,ncol=4)
>A
[,1] [,2] [,3] [,4]
[1,] NA NA NA NA
[2,] NA NA NA NA
[3,] NA NA NA NA
[4,] NA NA NA NA
> A[,]<-1:16
>A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16

Q2
> row_names = c("row1", "row2", "row3", "row4")
> col_names = c("col1", "col2", "col3", "col4")
> B = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
> print(B)
col1 col2 col3 col4
row1 1 2 3 4
row2 5 6 7 8
row3 9 10 11 12
row4 13 14 15 16

Q3
> F = matrix(c(1:16), nrow = 4, byrow = TRUE)
>F
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
> F[2,3]
[1] 7
> F[3,]
[1] 9 10 11 12
> F[,4]
[1] 4 8 12 16
Q4
> m1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
> print(m1)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> m2 <- matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
> print(m2)
[,1] [,2] [,3]
[1,] 0 2 0
[2,] 1 3 2
> addition<-print(m1+m2)
[,1] [,2] [,3]
[1,] 1 5 5
[2,] 3 7 8
> minus<-print(m1-m2)
[,1] [,2] [,3]
[1,] 1 1 5
[2,] 1 1 4
> multiply<-print(m1%*%t(m2))
[,1] [,2]
[1,] 6 20
[2,] 8 26
> print(solve(multiply))
[,1] [,2]
[1,] -6.5 5.0
[2,] 2.0 -1.5

Q5
> r1<-print(matrix(data=c(1,1,2), nrow=1, ncol=3))
[,1] [,2] [,3]
[1,] 1 1 2
> r2<-print(matrix(data=c(1,1,2), nrow=1, ncol=3))
[,1] [,2] [,3]
[1,] 1 1 2
> c1<-print(matrix(data=c(4,4), nrow=2, ncol=1))
[,1]
[1,] 4
[2,] 4
> r<-print(rbind(r1,r2))
[,1] [,2] [,3]
[1,] 1 1 2
[2,] 1 1 2
> G<-print(cbind(r,c1))
[,1] [,2] [,3] [,4]
[1,] 1 1 2 4
[2,] 1 1 2 4

Q6
> H<-print(matrix(c(1:9), nrow = 3, byrow = TRUE))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> print(H[H[,3]>5,])
[,1] [,2] [,3]
[1,] 4 5 6
[2,] 7 8 9

You might also like