For this exercise, first write down your answer, without using R. Then, check your answer using R.
4.1.2.1 If M=matrix(c(1:10),nrow=5,ncol=2,dimnames=list(c(‘a’,‘b’,‘c’,‘d’,‘e’),c(‘A’,‘B’))) What is the value of: M ?
# M looks like a 5*2 matrix with row names a,b,c,d & e and column names A and B. The values are 1 to 10. since byrow = FALSE by default, cells are filled by column with values 1 to 10 are.
M=matrix(c(1:10),nrow=5,ncol=2,dimnames=list(c('a','b','c','d','e'),c('A','B')))
M
## A B
## a 1 6
## b 2 7
## c 3 8
## d 4 9
## e 5 10
4.1.2.2 Consider the matrix M, what are the values of:
M[1,]
M[,1]
M[3,2]
M[‘e’,‘A’]
M[1,] #First row - each column: 1 6
## A B
## 1 6
M[,1] #First column - each row: 1 2 3 4 5
## a b c d e
## 1 2 3 4 5
M[3,2] # 8
## [1] 8
M['e','A'] # 5
## [1] 5
4.1.2.3 Consider the matrix N=matrix(c(1:9),nrow=3,ncol=3,dimnames=list(c(‘a’,‘b’,‘c’),c(‘A’,‘B’,‘C’)))
What is the value of: diag(N)
N=matrix(c(1:9),nrow=3,ncol=3,dimnames=list(c('a','b','c'),c('A','B','C')))
# 3*3 matrix. Cells with values 1 to 9. diag() simply gives the diagonal matrix of N.
diag(N)
## [1] 1 5 9
4.1.2.4 What is the value of: diag(4,3,3) Is matrix ?
diag(4,3,3) # Values on the diagonal line will be 4. It will be a 3*3 matrix.
## [,1] [,2] [,3]
## [1,] 4 0 0
## [2,] 0 4 0
## [3,] 0 0 4
4.1.2.5 If M=matrix(c(1:9),3,3,byrow=T,dimnames=list(c(‘a’,‘b’,‘c’),c(‘d’,‘e’,‘f’)))
What is the value of: rownames(M) and colnames(M)
M=matrix(c(1:9),3,3,byrow=T,dimnames=list(c('a','b','c'),c('d','e','f')))
rownames(M) # a b c
## [1] "a" "b" "c"
colnames(M) # d e f
## [1] "d" "e" "f"
4.1.2.6 What are the values of:
upper.tri(M)
lower.tri(M)
lower.tri(M,diag=T)
upper.tri(M) # TRUE for the cells on right hand side of diagonal line
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] FALSE FALSE TRUE
## [3,] FALSE FALSE FALSE
lower.tri(M) # TRUE for the cells on left hand side of diagonal line
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
lower.tri(M,diag=T) # TRUE for the cells on left hand side of diagonal line including the cells on diagonal line.
## [,1] [,2] [,3]
## [1,] TRUE FALSE FALSE
## [2,] TRUE TRUE FALSE
## [3,] TRUE TRUE TRUE
4.1.2.7 Consider two matrix, M=matrix(c(1:9),3,3,byrow=T) and N=matrix(c(1:9),3,3)
What is the value of M*N?
M=matrix(c(1:9),3,3,byrow=T) #3*3, filled by row
N=matrix(c(1:9),3,3) #3*3, filled by column
M*N # component-wise multiplication. M*N[2,1] should be 8 for example. Let's see:
## [,1] [,2] [,3]
## [1,] 1 8 21
## [2,] 8 25 48
## [3,] 21 48 81
4.1.2.8 Consider two matrix M=matrix(c(1:9),3,3,byrow=T) and N=matrix(c(1:9),3,3)
What is the value of M%*%N?
# Matrix multiplication. I'm too lazy to compute & compare it manually. But notice the different results with the previous exercise.
M%*%N
## [,1] [,2] [,3]
## [1,] 14 32 50
## [2,] 32 77 122
## [3,] 50 122 194
4.1.2.9 Consider two matrix, M=matrix(c(1:9),3,3,byrow=T) and N=matrix(c(1:9),3,3)
What is the value of (M+N)^2?
(M+N)^2
## [,1] [,2] [,3]
## [1,] 4 36 100
## [2,] 36 100 196
## [3,] 100 196 324
4.1.2.10 Consider two matrix M=matrix(c(1:9),3,3,byrow=T) and N=matrix(c(1:9),3,3)
What is the value of M/N?
M/N # Component-wise dividing
## [,1] [,2] [,3]
## [1,] 1.000000 0.500000 0.4285714
## [2,] 2.000000 1.000000 0.7500000
## [3,] 2.333333 1.333333 1.0000000