4.1.1 Create three vectors x,y,z with integers and each vector has 3 elements. Combine the three vectors to become a 3×3 matrix A where each column represents a vector. Change the row names to a,b,c.
Think: How about each row represents a vector, can you modify your code to implement it?
x <- sample(1:10,3,replace = TRUE)
y <- sample(1:10,3,replace = TRUE)
z <- sample(1:10,3,replace = TRUE)
A <- cbind(x,y,z)
A # Now we have a matrix but the row names are 1,2,3.
## x y z
## [1,] 6 2 4
## [2,] 10 10 8
## [3,] 2 3 1
rownames(A) <- c("a","b","c")
A
## x y z
## a 6 2 4
## b 10 10 8
## c 2 3 1
4.1.2 Please check your result from Exercise 1, using is.matrix(A). It should return TRUE, if your answer is correct. Otherwise, please correct your answer. Hint: Note that is.matrix() will return FALSE on a non-matrix type of input. Eg: a vector and so on.
is.matrix(A) # O yes it is.
## [1] TRUE
4.1.3 Create a vector with 12 integers. Convert the vector to a 4*3 matrix B using matrix(). Please change the column names to x, y, z and row names to a, b, c, d. The argument byrow in matrix() is set to be FALSE by default. Please change it to TRUE and print B to see the differences.
b <- sample(1:50,12,replace = FALSE)
B <- matrix(b, nrow = 4, ncol = 3)
rownames(B) <- c("a","b","c","d")
colnames(B) <- c("x","y","z")
B # The byrow argument specifies how the matrix is to be filled. The default value for byrow is FALSE which means that by default the matrix will be filled column by column.
## x y z
## a 45 48 29
## b 9 31 21
## c 24 1 2
## d 46 27 14
B <- matrix(b, nrow = 4, ncol = 3, byrow = TRUE)
B # See the difference
## [,1] [,2] [,3]
## [1,] 45 9 24
## [2,] 46 48 31
## [3,] 1 27 29
## [4,] 21 2 14
4.1.4 Please obtain the transpose matrix of B named tB .
tB <- t(B)
tB
## [,1] [,2] [,3] [,4]
## [1,] 45 46 1 21
## [2,] 9 48 27 2
## [3,] 24 31 29 14
4.1.5 Now tB is a 3×4 matrix. By the rule of matrix multiplication in algebra, can we perform tB*tB in R language? (Is a 3×4 matrix multiplied by a 3×4 allowed?) What result would we get?
tB*tB # R performs this multiplication. However, * give component-wise multiplication, but not the real matrix #multiplication defined by algebra rules.
## [,1] [,2] [,3] [,4]
## [1,] 2025 2116 1 441
## [2,] 81 2304 729 4
## [3,] 576 961 841 196
4.1.6 As we can see from Exercise 5, we were expecting that tB x tB would not be allowed because it disobeys the algebra rules. But it actually went through the computation in R. However, as we check the output result , we notice the multiplication with a single * operator is performing the componentwise multiplication. It is not the conventional matrix multiplication. How to perform the conventional matrix multiplication in R? Can you compute matrix A multiplies tB ?
# You should use %*% for conventional matrix multiplication.
A%*%tB
## [,1] [,2] [,3] [,4]
## a 384 496 176 186
## b 732 1188 512 342
## c 141 267 112 62
4.1.7 If we convert A to a data.frame type instead of a matrix, can we still compute a conventional matrix multiplication for matrix A multiplies matrix A? Is there any way we could still perform the matrix multiplication for two data.frame type variables? (Assuming proper dimension)
A <- data.frame(A)
#A %*% A ----Try this one first. You cannot compute a conventional matrix multiplication with data frames. But, you can multiply them with following expression:
as.matrix(A) %*% as.matrix(A)
## x y z
## a 64 44 44
## b 176 144 128
## c 44 37 33
4.1.8 Extract a sub-matrix from B named subB . It should be a 3×3 matrix which includes the last three rows of matrix B and their corresponding columns.
subB<-B[2:dim(B)[1],1:3] #dim(B) is 4 3. [1] gives 4.
subB
## [,1] [,2] [,3]
## [1,] 46 48 31
## [2,] 1 27 29
## [3,] 21 2 14
4.1.9 Compute 3*A , A+subB , A-subB . Can we compute A+B? Why?
3*A
## x y z
## a 18 6 12
## b 30 30 24
## c 6 9 3
A + subB
## x y z
## a 52 50 35
## b 11 37 37
## c 23 5 15
A - subB
## x y z
## a -40 -46 -27
## b 9 -17 -21
## c -19 1 -13
A+B # I don't know why but somehow, I'm able to do that without error. However, the result is completely wrong. Matrices' dimensions should match to complete this computation.
## x y z
## a 51 23 31
## b 56 19 10
## c 3 51 25
4.1.10 Generate a n * n matrix (square matrix) A1 with proper number of random numbers, then generate another n * m matrix A2.
If we have A1M=A2 (Here represents the conventional multiplication), please solve for M. Hint: use the runif() and solve() functions. E.g., runif(9) should give you 9 random numbers.
A1 <- matrix(runif(9),3,3)
A2 <- matrix(runif(12),3,4)
# solve(a,b,..) This function solves the equation a %*% x = b for x, where b can be either a vector or a matrix.
M <- solve(A1,A2)
M
## [,1] [,2] [,3] [,4]
## [1,] 24.170877 65.67209 40.13738 54.79820
## [2,] -20.762506 -59.61175 -36.58676 -48.80166
## [3,] 6.760614 20.25804 13.26208 16.37847