Assignment4 L13-L17
Assignment4 L13-L17
Assignment4 L13-L17
July 2024
Assignment 4
a. 61 65 69 73
b. 25 26 28 29
c. 42 46 50 54
d. 84 92 100 108
a. 61 65 69 73
b. 48 80 112 144
c. 24 40 56 72
d. 68 72 76 80
1
3. Which one of the following is the correct outcome of the command
rowMeans(X) for the matrix constituted by the command
X= matrix(seq(9,39,2),4,4,byrow=F)?
a. 21 23 25 27
b. 62 66 70 74
c. 26 27 27 28
d. 25 26 27 28
2
5. Which one of the following is the correct outcome matrix of the command
6 7 8
a. 𝑥 = (0 7 8)
0 0 8
0 0 6
b. 𝑥 = (0 7 0)
8 0 0
6 7 8
c. 𝑥 = (7 6 7)
8 7 6
6 0 0
d. 𝑥 = (0 7 0)
0 0 8
Solution: The diag() command is used to generate a diagonal matrix with diagonal
elements given by the data vector.
3
6. Which one of the following is the correct outcome of the command t(x) for
a.
[,1] [,2] [,3] [,4]
[1,] 18 16 14 12
[2,] 17 15 13 11
b.
[,1] [,2]
[1,] 18 17
[2,] 16 15
[3,] 14 13
[4,] 12 11
c.
[,1] [,2] [,3] [,4]
[1,] 17 15 13 11
[2,] 18 16 14 12
d.
[,1] [,2]
[1,] 17 18
[2,] 15 16
[3,] 13 14
[4,] 11 12
Solution: After generating a matrix x we use the command t(x) to get the transpose.
In this case the matrix x is of 4*2 dimension, so the t(x) is of 2*4 dimension.
4
7. Which one of the following is the correct command to obtain the multiplication of
two square matrices x and y of compatible order?
a. x**y
b. x*%*y
c. x%*%y
d. x%**%y
Solution: The matrix multiplication of two matrices x and y can be obtained by the
command x%*%y if matrices x and y are of compatible dimension. Here, the
matrices are supposed to be square and same order, so they are also compatible.
8. Which one of the following is the correct command to obtain the multiplication of
6 1 3 3 5 8
two matrices 𝑥 = (8 6 2) and 𝑦 = (4 1 6) along with its correct answer?
7 1 9 7 6 3
a. x*y
and its correct answer is
[,1] [,2] [,3]
[1,] 28 4 36
[2,] 32 12 8
[3,] 49 24 18
b. x*%*y
and its correct answer is
[,1] [,2] [,3]
[1,] 123 67 117
[2,] 60 32 56
[3,] 90 51 58
c. x%*%y
5
and its correct answer is
[,1] [,2] [,3]
[1,] 43 49 63
[2,] 94 56 100
[3,] 128 90 97
d. x%%*%%y
and its correct answer is
[,1] [,2] [,3]
[1,] 28 4 36
[2,] 32 12 8
[3,] 49 24 18
Solution: The matrix multiplication of two matrices x and y can be obtained by the
command x%*%y if matrices x and y are of compatible dimension. Here, the
matrices are supposed to be square and same order, so they are also compatible.
a.
[,1] [,2] [,3] [,4]
[1,] 72 75 78 81
[2,] 84 87 90 93
b.
[,1] [,2] [,3] [,4]
[1,] 80 84 88 92
[2,] 64 68 72 76
c.
[,1] [,2]
[1,] 72 84
[2,] 75 87
6
[3,] 78 90
[4,] 81 93
d.
[,1] [,2]
[1,] 84 72
[2,] 87 75
[3,] 90 78
[4,] 93 81
10. Which one of the following is the correct command to obtain of the addition of
4 1 4 7 4 9
two matrices 𝑥 = (8 6 2) and 𝑦 = (4 2 4) along with its correct answer?
7 4 9 7 6 2
a. x + y
and its correct answer is
[,1] [,2] [,3]
[1,] 11 5 13
[2,] 12 8 6
[3,] 14 10 11
b. x %+% y
and its correct answer is
[,1] [,2] [,3]
[1,] 11 5 13
[2,] 12 8 6
[3,] 14 10 11
c. x + y
and its correct answer is
[,1] [,2] [,3]
[1,] 11 12 14
7
[2,] 5 8 10
[3,] 13 6 11
d. x %+% y
and its correct answer is
[,1] [,2] [,3]
[1,] 11 12 14
[2,] 5 8 10
[3,] 13 6 11
Solution: The matrix addition of two matrices x and y can be obtained by the
command x+y if matrices x and y are of same dimension.
a.
[,1] [,2] [,3] [,4]
[1,] -66 -78 -90 -102
[2,] -69 -81 -93 -105
[3,] -72 -84 -96 -108
[4,] -75 -87 -99 -111
b.
[,1] [,2] [,3] [,4]
[1,] -66 -69 -72 -75
[2,] -78 -81 -84 -87
[3,] -90 -93 -96 -99
[4,] -102 -105 -108 -111
c. Error...
8
d. None of these
Solution: x%*%y is the command for matrix multiplication of two matrices x and y if
matrices x and y are of compatible dimension. Here, the scalar 3 and matrix x are
used in this command 3%*%x, so it given an error.
12. Which one of the following is the correct outcome of X[3 ,] for the matrix
specified by X= matrix(nrow=4, ncol=4, data=5:20, byrow=F) ?
a. [1] 7 11 15 19
b. [1] 8 12 16 20
c. [1] 13 14 15 16
d. None of these
13. Which one of the following is the correct outcome of x[3:4,1:2] for the matrix
specified by x= matrix(nrow=4, ncol=4, data=5:20, byrow=F)?
a.
[,1] [,2]
[1,] 8 12
[2,] 11 7
b.
[,1] [,2]
[1,] 7 11
[2,] 8 12
c.
9
[,1] [,2]
[1,] 7 8
[2,] 11 12
d.
[,1] [,2]
[1,] 27 28
[2,] 31 32
4 6
14. Which one of the following is the correct command to get the matrix ( ) from
7 9
the matrix specified by
a. X[2:3, 1:3]
b. X[2:3, c(1,3)]
c. X[1:3, c(2,3)]
d. X[3:3, 2:2, 1:1]
10
15. Which one of the following is the correct output of the command cbind(x,y)
for the matrices specified by
a.
b.
c.
d.
11
16. Which one of the following is the correct output of the command rbind(x,y)
for the matrices specified by
a.
b.
c.
d.
12
3 1 2 5
4 2 4 6
17. If 𝑥 = ( ) then which one of the following is the correct
5 3 6 7
6 8 2 8
command and its outcome for obtaining the inverse of the sub-matrix of x formed by
first, second, and third columns and second, third, and fourth rows?
a.
solve(x[2:4,1:3])=
[,1] [,2] [,3]
[1,] 1.5000000 -1.0000000 0.00000000
[2,] -0.9285714 0.5714286 0.14285714
[3,] -0.7857143 0.7142857 -0.07142857
b.
inv(x[1:3,2:4])=
[,1] [,2] [,3]
[1,] 1.5000000 -1.0000000 0.00000000
[2,] -0.7857143 -0.7142857 -0.07142857
[3,] -0.9285714 0.5714286 0.14285714
c.
inv(x[1:3,2:4])=
[,1] [,2] [,3]
[1,] 1.5000000 -1.0000000 0.00000000
[2,] -0.9285714 0.5714286 0.14285714
[3,] -0.7857143 -0.7142857 -0.07142857
d.
solve((x[1:3,2:4]))=
[,1] [,2] [,3]
[1,] -0.02777778 1.6944444 -3.8611111
[2,] 0.05555556 0.1111111 -0.2777778
[3,] -0.02777778 -1.3055556 3.1388889
13
Solution: Command solve(x) is to obtain the inverse of matrix x. Consequently,
solve(x[2:4,1:3]) is used to obtain the inverse of submatrix of x obtained by column
numbers 1 to 3, and row numbers 2 to 4.
18. Which one of the following is the correct outcome of the command
(x < 25) || (x > 22) & (x < 25) && (x > 22)||(x == 27) when
x = 21 and when x = 45 ?
Solution: Each part of the command separated by logical operators ||, &, && for x=23
gives an output of TRUE. For x=45, since (x<25) gives an output of FALSE and it is
followed by && operator, the overall result is FALSE.
19. Suppose x = 3:6. Then which one of the following is the correct outcome of the
command
14
Solution: Only 6 satisfies both parts of the command. So, only one TRUE at the
position of 6 is obtained. Rest of the elements are FALSE.
20. Suppose x = 13:28. Then which one of the following correctly specifies the
outcome of the following statement: x[(x > 22) & (x < 27)] and x[(x >
25) | (x < 15)] ?
21. Suppose x = 20:60. Then which one of the following is the correct command
to know that which of the values in x are more than 30 and less than 50?
15
22. Suppose x = 55. Then which one of the following is the correct outcome
of (x > 30) & (x < 50) and (x > 60) | (x < 70) ?
23. Suppose x = 23:27 then which one of the following is the correct outcome of
a. TRUE
b. FALSE
d. 27 26 25 24 23
16
24. Suppose x = c(15,25,35,45,55) and y = c(45,55,65,75,85) then
which one of the following is the correct outcome of x != y ?
c. TRUE
d. FALSE
c. TRUE
d. FALSE
17
MOOC Course - Foundations of R Software
Answers of Assignment 4
1. d
2. a
3. b
4. a
5. d
6. a
7. c
8. c
9. a
10. a
11. c
12. a
13. b
14. b
15. c
16. d
17. a
18. d
19. a
20. a
21. a
22. d
23. c
24. a
25. b
18
19