MTH-2520 Homework 6: Matrices and Arrays


Problem 1.

Create an example matrix in the three following ways:

Problem 1a Using matrix()

matrix(1:5, nrow = 5, ncol=8)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    1    1    1    1    1    1
[2,]    2    2    2    2    2    2    2    2
[3,]    3    3    3    3    3    3    3    3
[4,]    4    4    4    4    4    4    4    4
[5,]    5    5    5    5    5    5    5    5

Problem 1b Using rbind()

matrix_1b1 <- matrix(1, nrow = 1, ncol=8, byrow=FALSE) 
matrix_1b2 <- matrix(2, nrow = 1, ncol=8, byrow=FALSE)
matrix_1b3 <- matrix(3, nrow = 1, ncol=8, byrow=FALSE)
matrix_1b4 <- matrix(4, nrow = 1, ncol=8, byrow=FALSE)
matrix_1b5 <- matrix(5, nrow = 1, ncol=8, byrow=FALSE)

rbind(matrix_1b1, matrix_1b2, matrix_1b3, matrix_1b4, matrix_1b5)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    1    1    1    1    1    1
[2,]    2    2    2    2    2    2    2    2
[3,]    3    3    3    3    3    3    3    3
[4,]    4    4    4    4    4    4    4    4
[5,]    5    5    5    5    5    5    5    5

Problem 1c Using cbind()

matrix_1c1 <- matrix(1:5, nrow = 5, ncol=4) 
matrix_1c2 <- matrix(1:5, nrow = 5, ncol=4)

cbind(matrix_1c1, matrix_1c2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    1    1    1    1    1    1
[2,]    2    2    2    2    2    2    2    2
[3,]    3    3    3    3    3    3    3    3
[4,]    4    4    4    4    4    4    4    4
[5,]    5    5    5    5    5    5    5    5

Problem 2.

For the matrix A in pr. 1: , b)

Problem 2a.

add a row of 6’s on the bottom

matrix_2 <- matrix(1:5, nrow = 5, ncol=8)
rbind(matrix_2, rep(6, 8))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    1    1    1    1    1    1
[2,]    2    2    2    2    2    2    2    2
[3,]    3    3    3    3    3    3    3    3
[4,]    4    4    4    4    4    4    4    4
[5,]    5    5    5    5    5    5    5    5
[6,]    6    6    6    6    6    6    6    6

Problem 2b.

then extract the middle 2 × 4 matrix

# Before we can do this we need to know what the middle even is! This matrix is 6 rows long and 8 columns long. That means the middle 2 x 4 matrix would be in rows 3-4 and columns 3-6. We should get all 3s and 4s in our matrix.

matrix_2[row=3:4, col=3:6]
     [,1] [,2] [,3] [,4]
[1,]    3    3    3    3
[2,]    4    4    4    4

Problem 3

Build a 10 x 10 identity matrix. Then change all the non-zero elements to 5.

matrix_3 <- diag(10)
matrix_3 * 5
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    5    0    0    0    0    0    0    0    0     0
 [2,]    0    5    0    0    0    0    0    0    0     0
 [3,]    0    0    5    0    0    0    0    0    0     0
 [4,]    0    0    0    5    0    0    0    0    0     0
 [5,]    0    0    0    0    5    0    0    0    0     0
 [6,]    0    0    0    0    0    5    0    0    0     0
 [7,]    0    0    0    0    0    0    5    0    0     0
 [8,]    0    0    0    0    0    0    0    5    0     0
 [9,]    0    0    0    0    0    0    0    0    5     0
[10,]    0    0    0    0    0    0    0    0    0     5

So, funfact for this one. I was racking my brain thinking of some way to change the values in the matrix based on a conditional (ie: where(x != 0)). My roommate walks over, looks at my screen and says “why don’t you just multiply the matrix by 5”. Right. Of course. The solution that’s directly in the lab notes. I should probably go with that solution.


Problem 4.

Linear Algebra

Problem 4a.

Use the random number generator rnorm() with the given seed to generate A.

set.seed(1234)
A <- matrix(rnorm(25), nrow = 5)
A
           [,1]       [,2]        [,3]       [,4]       [,5]
[1,] -1.2070657  0.5060559 -0.47719270 -0.1102855  0.1340882
[2,]  0.2774292 -0.5747400 -0.99838644 -0.5110095 -0.4906859
[3,]  1.0844412 -0.5466319 -0.77625389 -0.9111954 -0.4405479
[4,] -2.3456977 -0.5644520  0.06445882 -0.8371717  0.4595894
[5,]  0.4291247 -0.8900378  0.95949406  2.4158352 -0.6937202

Problem 4b.

Calculate the determinant of A.

det(A)
[1] -0.562265

Problem 4c.

Calculate the inverse of A.

solve(A)
          [,1]      [,2]       [,3]       [,4]       [,5]
[1,] -1.422040  1.438988  -1.628982 -0.3694698 -0.5029832
[2,]  2.903350 -3.505745   3.563584 -0.1352708  0.6882113
[3,]  2.850793 -5.321075   5.452092  0.6602019  1.2897834
[4,] -2.065381  3.466124  -4.011938 -0.4977805 -0.6328853
[5,] -7.854210 10.098876 -12.010162 -0.8753507 -3.0556750

Problem 5.

For the given matrix compute each part separately. If you alter the matrix be sure to reset it back to its default value.

\(A = \begin{bmatrix} 1 & 1 & 1 \\ 5 & 2 & 6 \\ -2 & -1 & -3 \end{bmatrix}\)

p5_vector <- c(1,1,3,5,2,6,-2,-1,-3)
p5_matrix <- matrix(p5_vector, nrow = 3, ncol = 3, byrow=TRUE) 
p5_matrix
     [,1] [,2] [,3]
[1,]    1    1    3
[2,]    5    2    6
[3,]   -2   -1   -3

Problem 5a.

Compute \(A^2\)

p5_matrix %*% p5_matrix
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    3    3    9
[3,]   -1   -1   -3

Problem 5b.

Compute A^2. What is R computing here?

p5_matrix^2
     [,1] [,2] [,3]
[1,]    1    1    9
[2,]   25    4   36
[3,]    4    1    9

p5_matrix^2 is doing element-wise multiplication. That is, element [1,1] is multiplied with element [1,1], element [1,2] with element [1,2] and so on. In other words, every element is multiplied with itself. This could not be more different from matrix multiplication.

Problem 5c.

Verify that \(A^3 = 0\) where \(0\) is a 3 X 3 matrix with every entry equal to 0.

p5_matrix %*% p5_matrix %*% p5_matrix
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0

Problem 5d.

Compute A^3. What is R computing here?

p5_matrix^3
     [,1] [,2] [,3]
[1,]    1    1   27
[2,]  125    8  216
[3,]   -8   -1  -27

Yet again we’re seeing the result of element wise multiplication. Each element is simply multiplied with itself twice.

Problem 5e.

Replace the 3rd column of A with the sum of the 2nd and 3rd columns.

columns_2_3 <- p5_matrix[,2] + p5_matrix[,3]

p5_matrix[,1] <- columns_2_3
p5_matrix
     [,1] [,2] [,3]
[1,]    4    1    3
[2,]    8    2    6
[3,]   -4   -1   -3

Problem 5f

Compute the column means of “A”.

cat("the column means of p5_matrix are [", colMeans(p5_matrix), "] \n")
the column means of p5_matrix are [ 2.666667 0.6666667 2 ] 

Problem 5g

Compute the square roots of the 2nd column.

cat("The square roots of column two are [", sqrt(as.complex(p5_matrix[,2])), "] \n")
The square roots of column two are [ 1+0i 1.414214+0i 0+1i ] 

Problem 5h

Subtract 5 from column 1, subtract 4 from column 2 and add 9 to column 3 of A. Use sweep().

sweep(p5_matrix, MARGIN = 2, c(-5, -4, 9), "+") #Margin=2 in this case because we're manipulating by column not by row. 
     [,1] [,2] [,3]
[1,]   -1   -3   12
[2,]    3   -2   15
[3,]   -9   -5    6

Problem 6

Use the outer() function to make a table (actually a matrix) of powers of integers \(x^y\) where x <- 1:10 and y <- 1:9

pr6_x <- 1:10
pr6_y <- 1:9

outer(pr6_x, pr6_y, FUN = "^")
      [,1] [,2] [,3]  [,4]   [,5]    [,6]     [,7]      [,8]       [,9]
 [1,]    1    1    1     1      1       1        1         1          1
 [2,]    2    4    8    16     32      64      128       256        512
 [3,]    3    9   27    81    243     729     2187      6561      19683
 [4,]    4   16   64   256   1024    4096    16384     65536     262144
 [5,]    5   25  125   625   3125   15625    78125    390625    1953125
 [6,]    6   36  216  1296   7776   46656   279936   1679616   10077696
 [7,]    7   49  343  2401  16807  117649   823543   5764801   40353607
 [8,]    8   64  512  4096  32768  262144  2097152  16777216  134217728
 [9,]    9   81  729  6561  59049  531441  4782969  43046721  387420489
[10,]   10  100 1000 10000 100000 1000000 10000000 100000000 1000000000

Problem 7

Problem 7a

Create a 6 X 10 matrix of random integers from 1:10.

set.seed(75)
p7_matrix <- matrix(sample(x = 10, size = 60, replace = TRUE), nrow = 6) 
p7_matrix
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    8    8    8    7    7    5    2    2    6     5
[2,]    9    5    2    6    6    1    6    6    3     7
[3,]    5    1   10    2    5    6    8    9   10     8
[4,]    9    3    1    1    6   10   10    7    9    10
[5,]    7    3    3    3    6    4    4    6   10     2
[6,]    9    3    3    4    1    2    1   10    6     1

Problem 7b

Find the number of entries in each row which are greater than 4.

# length(which(p7_matrix[1,] > 4))

greater_than_4 <- function(matrix) {
  # numrows is set to the amount of rows in a given matrix.
  numrows <- nrow(matrix)
  # We set our return vector to an empty vector that we will fill later.
  ret_vec <- c()
  # This for loop goes from 1 to the number provided by numrows.
  for (row in c(1:numrows)){
    # We filter out the amount of numbers greater than 4 per row and then count how many there are. 
    filter_var <- length(which(matrix[row,] > 4))
    # We then append that counted length to our empty vector
    ret_vec <- append(ret_vec, filter_var)
  } 
  return(ret_vec)
}

greater_than_4(p7_matrix)
[1] 8 7 8 7 4 3

I, for the life of me, could not figure out how to do this without a for loop. I’m positive there has to be a cleaner way to do this problem but I spent forever on it with no luck.

Problem 7c

Find which rows contain exactly two occurrences of the number 7.

sevens <- function(matrix) {
  
  numrows <- nrow(matrix)
  for (row in c(1:numrows)){
    # This sets seven_var to the length of a vector made up of however many times a value in a row is equal to 7.
    sevens_var <- length(which(matrix[row,] == 7))
    # This returns the row which gave us a sevens_var value of 2.
    if(sevens_var == 2){return(row)}  
  } 
}

cat("The row in p7_matrix that has two occurrences of the number 7 is row", sevens(p7_matrix), "\n")
The row in p7_matrix that has two occurrences of the number 7 is row 1 

Problem 7d

Use apply() to sort each column of A in decreasing order

apply(p7_matrix, MARGIN = 2, FUN = sort, decreasing=TRUE)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    9    8   10    7    7   10   10   10   10    10
[2,]    9    5    8    6    6    6    8    9   10     8
[3,]    9    3    3    4    6    5    6    7    9     7
[4,]    8    3    3    3    6    4    4    6    6     5
[5,]    7    3    2    2    5    2    2    6    6     2
[6,]    5    1    1    1    1    1    1    2    3     1

Problem 7e

Use order() to sort A in decreasing order based on column 1.

# p7_matrix
a <- p7_matrix[order(p7_matrix[,1], decreasing=TRUE),]

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

This mysterious floating comma at the end being the one thing making this work correctly is absolutely blowing my mind right now.

Problem 7f

Use apply() and the transpose to sort each row of A in increasing order.

mat_7f <- t(p7_matrix) 
apply(mat_7f, MARGIN = 1, FUN = sort, decreasing=FALSE)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    5    1    1    1    1    1    1    2    3     1
[2,]    7    3    2    2    5    2    2    6    6     2
[3,]    8    3    3    3    6    4    4    6    6     5
[4,]    9    3    3    4    6    5    6    7    9     7
[5,]    9    5    8    6    6    6    8    9   10     8
[6,]    9    8   10    7    7   10   10   10   10    10

Problem 7g

Use order() to sort A in increasing order based on row 3

# p7_matrix
b <- p7_matrix[, order(p7_matrix[3,], decreasing = FALSE)]

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

It took me a solid hour to figure out that I needed the comma after the [] there instead of at the end. That is, quite possibly, the silliest thing I have seen in my life (so far). The mysterious floating comma will live on in my nightmares. I fear I have not yet seen the last of its treachery.


Problem 8.

Find the sum

Problem 8a.

\[\sum_{i=1}^{20} \sum_{j=1}^{15} (2i + 3j)\]

# First we'll create our two elements, i and j.
var_8a_x <- c(1:20) * 2
var_8a_y <- c(1:15) * 3

sum(outer(var_8a_x, var_8a_y, FUN = "+"))
[1] 13500

Problem 8b.

\[ \sum_{i=1}^{20} \sum_{j=1}^{5} \frac{i^4}{3 + j} \]

# We'll use a similar breakdown for this guy.
var_8b_x <- c(1:20)^4
var_8b_y <- c(1:5) + 3

sum(outer(var_8b_x, var_8b_y, FUN = "/"))
[1] 639215.3

Problem 9

Write a function that takes a matrix and returns a matrix with every odd number doubled.

doubling_odds <- function(x){
  ifelse(x %% 2 != 0, x * 2, x)
}

pr_9a <- matrix(c(1,-3,-2,4), nrow=2)
pr_9b <- matrix(c(1:15), nrow = 3)

doubling_odds(pr_9a)
     [,1] [,2]
[1,]    2   -2
[2,]   -6    4
doubling_odds(pr_9b)
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    4   14   10   26
[2,]    2   10    8   22   14
[3,]    6    6   18   12   30

Problem 10.

Problem 10a.

Write a function that takes a matrix and returns the smallest number in the matrix.

smallest_number <- function(matrix) {
  # This sets var_10a as a given matrix sorted in ascending order
  var_10a <- sort(matrix, decreasing=FALSE,)
  # This returns the first index in said matrix, which due to the above will be the smallest number.
  return(var_10a[1])
  } 

Problem 10b.

Test the function on the following matrices.

matrix_10b1 <- matrix(c(1, -3, -2, 4), nrow = 2)
smallest_number(matrix_10b1)
[1] -3
smallest_number(A)
[1] -2.345698

Problem 11

Solve the system of linear equations. Verify that \(det A \neq 0\).

matrix_11_left <- matrix(c(4, 1, 2, -3, -3, 3, -1, 4, -1, 2, 5, 1, 5, 4, 3, -1), byrow = TRUE, nrow = 4)
matrix_11_right <- matrix(c(-16, 20, -4, -10), nrow=4)

cat("det A is equal to", det(matrix_11_left, matrix_11_right), "\n")
det A is equal to 116 
matrix(solve(matrix_11_left, matrix_11_right), dimnames = list(c("w =", "x =", "y =", "z =")))
    [,1]
w =   -1
x =    1
y =   -2
z =    3
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy