Assignment4 L13-L17

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

MOOC Course - Foundations of R Software

July 2024

Assignment 4

1. Which one of the following is the correct outcome of the command


rowSums(X) for the matrix formed by the command
X= matrix(seq(9,39,2),4,4,byrow=F)?

a. 61 65 69 73
b. 25 26 28 29
c. 42 46 50 54
d. 84 92 100 108

Solution: Command for obtaining row sums in a matrix is rowSums().

2. Which one of the following is the correct outcome of the command


colSums(X) for the matrix constituted by the command
X= matrix(seq(9,39,2),4,4,byrow=F)?

a. 61 65 69 73
b. 48 80 112 144
c. 24 40 56 72
d. 68 72 76 80

Solution: Command for obtaining column sums in a matrix is colSums().

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

Solution: Command for obtaining mean of row elements in a matrix is rowMeans().

4. Which one of the following is the correct outcome of the command


colMeans(X) for the matrix constituted by the command
X= matrix(seq(9,39,2),4,4,byrow=F)?
a. 67 68 69 70
b. 12 20 28 36
c. 26 27 28 29
d. 21 23 25 27

Solution: Command for obtaining mean of column elements in a matrix is colMeans()

2
5. Which one of the following is the correct outcome matrix of the command

x = diag(c(6,7,8), nrow=3, ncol=3) ?

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

x=matrix(nrow=4, ncol=2, data=18:11, byrow=T) ?

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.

9. Let x=matrix(nrow=2, ncol=4, data=24:31, byrow=T) then which


one of the following is the correct outcome of 3*x ?

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

Solution: 3*x command for a matrix x outputs a scalar multiplication of x by 3.

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.

11. Let x= matrix(nrow=4, ncol=4, data=24:39, byrow=F) then which


one of the following is the correct outcome of 12-3%*%x ?

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

Solution: Command X[nrow, ] is used to obtain nrow-th row of the matrix X.

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

Solution: Command X[3:4, 1:2] is used to obtain a submatrix of the matrix X


containing elements from rows 3 to 4 and columns 1 to 2.

4 6
14. Which one of the following is the correct command to get the matrix ( ) from
7 9
the matrix specified by

X=matrix(nrow=4, ncol=3, data=1:12, byrow=T) ?

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]

Solution: Command X[2:3, c(1,3)] is used to obtain a submatrix of the matrix X


containing elements from rows 2 to 3 and columns 1 and 3.

10
15. Which one of the following is the correct output of the command cbind(x,y)
for the matrices specified by

x = matrix(nrow=2, ncol=3, data=1:6, byrow=F)

y = matrix(nrow=2, ncol=3, data=7:12, byrow=F) ?

a.

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


[1,] 1 5 6
[2,] 2 6 7
[3,] 3 7 8
[4,] 4 8 9

b.

[,1] [,2] [,3] [,4] [,5] [,6]


[1,] 1 2 3 4 5 6
[2,] 7 8 9 10 11 12

c.

[,1] [,2] [,3] [,4] [,5] [,6]


[1,] 1 3 5 7 9 11
[2,] 2 4 6 8 10 12

d.

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


[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12

Solution: Command cbind(x,y) is used to obtain a matrix obtained by binding two


matrices x and y column-wise (putting columns of y after columns of x).

11
16. Which one of the following is the correct output of the command rbind(x,y)
for the matrices specified by

x=matrix(nrow=2, ncol=3, data=1:6, byrow=F)

y=matrix(nrow=2, ncol=3, data=7:12, byrow=F) ?

a.

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


[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12

b.

[,1] [,2] [,3] [,4] [,5] [,6]


[1,] 1 3 5 2 4 6
[2,] 7 9 11 8 10 12

c.

[,1] [,2] [,3] [,4] [,5] [,6]


[1,] 7 9 11 1 3 5
[2,] 8 10 12 2 4 6

d.

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


[1,] 1 3 5
[2,] 2 4 6
[3,] 7 9 11
[4,] 8 10 12

Solution: Command rbind(x,y) is used to obtain a matrix obtained by binding two


matrices x and y row-wise (putting rows of y after rows of x).

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.

x = matrix(data = c(3,1,2,5,4,2,4,6,5,3,6,7,6,8,2,8), nrow = 4, ncol = 4, byrow = T)

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 ?

a. FALSE and FALSE respectively.

b. TRUE and TRUE respectively.

c. FALSE and TRUE respectively.

d. TRUE and FALSE respectively.

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

(x > 5)&(x < 8) ?

a. FALSE FALSE FALSE TRUE

b. TRUE FALSE TRUE TRUE

c. TRUE TRUE TRUE FALSE

d. FALSE FALSE TRUE TRUE

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)] ?

a. are different for both as 23 24 25 26 and 13 14 26 27 28 respectively.

b. are the same for both as 23 24 25 26

c. are different for both as 13 14 26 27 28 and 23 24 25 26 respectively.

d. are the same for both as 13 14 26 27 28

Solution: Command x[logical vector] outputs a sub-vector of x with only elements


where T appears in logical vector.

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?

a. x[(x > 30) & (x < 50)]

b. (x > 30) & (x < 50)

c. x[(x >= 30) & (x >= 50)]

d. x[(x <= 30) & (x <= 50)]

Solution: Command x[logical vector] outputs a sub-vector of x with only elements


where T appears in logical vector. The logical vectors in options are self-explanatory.

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) ?

a. TRUE and FALSE respectively.

b. FALSE and FALSE respectively.

c. TRUE and TRUE respectively.

d. FALSE and TRUE respectively.

Solution: The logical vectors in the question are self-explanatory

23. Suppose x = 23:27 then which one of the following is the correct outcome of

-x[(x > 22) | (x < 25)] ?

a. TRUE

b. FALSE

c. -23 -24 -25 -26 -27

d. 27 26 25 24 23

Solution: Command x[logical vector] outputs a sub-vector of x with only elements


where T appears in logical vector. Taking a negative of the command outputs a
vector with each element’s sign reversed.

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 ?

a. TRUE TRUE TRUE TRUE TRUE

b. FALSE FALSE FALSE FALSE FALSE

c. TRUE

d. FALSE

Solution: Command x != y outputs a logical vector having elements T of


corresponding elements of x and y are not equal and vice-versa.

25. 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 ?

a. TRUE TRUE TRUE TRUE TRUE

b. FALSE FALSE FALSE FALSE FALSE

c. TRUE

d. FALSE

Solution: Command x == y outputs a logical vector having elements T of


corresponding elements of x and y are equal and vice-versa.

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

You might also like