## Exercises - Matrices

1. The following code creates in R the $5 \times 5$ matrix shown below.

$$\begin{bmatrix} 11 & 12 & 13 & 14 & 15\\ 21 & 22 & 23 & 24 & 25\\ 31 & 32 & 33 & 34 & 35\\ 41 & 42 & 43 & 44 & 45\\ 51 & 52 & 53 & 54 & 55 \end{bmatrix}$$
> m = rbind(c(11,12,13,14,15),
c(21,22,23,24,25),
c(31,32,33,34,35),
c(41,42,43,44,45),
c(51,52,53,54,55))


Using the above, write R code to generate the following sub-matrices of m:

1. the sub-matrix of elements in rows 3, 4, or 5 and in columns 1 or 2

2. the sub-matrix formed by deleting columns 2 and 4

3. the sub-matrix of rows whose first column element has an even ten's digit

4. the sub-matrix of elements in column 5 that correspond to an element in the same row and the first column that is less than 50. (Hint: the result should be a column matrix, not a vector.)

# (a)
> m[3:5,1:2]
[,1] [,2]
[1,]   31   32
[2,]   41   42
[3,]   51   52

# (b)
> m[,-c(2,4)]
[,1] [,2] [,3]
[1,]   11   13   15
[2,]   21   23   25
[3,]   31   33   35
[4,]   41   43   45
[5,]   51   53   55

# (c)
> m[(m[,1] %/% 10) %% 2 == 0,]
[,1] [,2] [,3] [,4] [,5]
[1,]   21   22   23   24   25
[2,]   41   42   43   44   45

# (d)
> m[m[,1]<50,5,drop=FALSE]
[,1]
[1,]   15
[2,]   25
[3,]   35
[4,]   45

2. Use R to determine the matrix product (in the traditional mathematical sense) of the following matrics:

$$A = \begin{bmatrix} 7 & 8 & 2 \\ 5 & 9 & 1 \\ 4 & 6 & 3 \end{bmatrix} \quad \textrm{ and } \quad B = \begin{bmatrix} 13 & -17\\ 0 & 19\\ -3 & -5 \end{bmatrix}$$

There are many ways to construct the matrices in question before multiplying them together. Below, we use the matrix() function to create matrix $A$ and the cbind() function to create matrix $B$. Note that byrow=FALSE in the matrix() function by default, so we don't have to specify it when we list the matrix contents by column. Also remember there is a rbind() function we could have used too.

> A = matrix(c(7,5,4,8,9,6,2,1,3),nrow=3)
> B = cbind(c(13,0,-3),c(-17,1,-5))
> A %*% B
[,1] [,2]
[1,]   85 -121
[2,]   62  -81
[3,]   43  -77

3. Matrices can be used to solve systems of linear equations. For example, one can solve for $x$, $y$, and $z$ in the system of equations \begin{align*} ax + by + cz &= p\\ dx + ey + fz &= q\\ gx + hy + iz &= r\\ \end{align*} by finding the product $$\begin{bmatrix}x\\y\\z\end{bmatrix} = \begin{bmatrix} a & b & c\\ d & e & f\\ g & h & i \end{bmatrix}^{-1} \cdot \begin{bmatrix}p\\q\\r\end{bmatrix}$$ Use a similar calculation to solve the following system:

\begin{align*} 2x-y+5z+w &= -3\\ 3x+2y+2z-6 &= -32\\ x+3y+3z-w &= -47\\ 5x-2y-3z+3w &= 49 \end{align*}
> M = rbind(c(2,-1,5,1),
c(3,2,2,-6),
c(1,3,3,-1),
c(5,-2,-3,3))

> C = rbind(-3,-32,-47,49)

> solve(M) %*% C
[,1]
[1,]    2
[2,]  -12
[3,]   -4
[4,]    1

Alternatively, one can simply execute the following and get the same answer:
> solve(M,C)


So $x=2, y=-12, z=-4, \textrm{ and } w=1$.

4. Suppose the following matrix is defined in R: $$\bf{A} = \begin{bmatrix}1 & 1 & 3\\5 & 2 & 6\\-2 & -1 & -3\end{bmatrix}$$

1. Use R to verify that $\bf{A}^3 = \bf{0}$ where $\bf{0}$ is a $3 \times 3$ matrix with every entry equal to $0$.

2. Use R to create a new matrix $\bf{B}$ equal to $\bf{A}$, except that its third column has been replaced by the sum of its second and third columns.

Check (a) with

A %*% A %*% A


For (b), the matrix B should display as the following:

> B
[,1] [,2] [,3]
[1,]    1    1    4
[2,]    5    2    8
[3,]   -2   -1   -4

5. Create a $6 \times 10$ matrix of random integers chosen from $1,2,\ldots,10$ by executing the following two lines of code:

set.seed(75)
m = matrix( sample(10, size=60, replace=T), nrows=6)


Use R to find the number of entries in this matrix which are greater than 4.

6. sum(m>4)

7. Create the following patterned matrices. In each case your solution should make use of the special form of the matrix -- this means that the solution should easily generalize to creating a larger matrix with the same structure and should not involve typing in all the entries of the matrix.

(a)   $\displaystyle{ \begin{bmatrix} 0 & 1 & 2 & 3 & 4\\ 1 & 2 & 3 & 4 & 0\\ 2 & 3 & 4 & 0 & 1\\ 3 & 4 & 0 & 1 & 2\\ 4 & 0 & 1 & 2 & 3 \end{bmatrix}}$   (b)   $\displaystyle{ \begin{bmatrix} 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\ 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 0\\ \vdots & \vdots &\vdots &\vdots &\vdots &\vdots &\vdots &\vdots &\vdots &\vdots &\\ 8 & 9 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7\\ 9 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \end{bmatrix}}$

(c)   $\displaystyle{ \begin{bmatrix} 0 & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1\\ 1 & 0 & 8 & 7 & 6 & 5 & 4 & 3 & 2\\ 2 & 1 & 0 & 8 & 7 & 6 & 5 & 4 & 3\\ 3 & 2 & 1 & 0 & 8 & 7 & 6 & 5 & 4\\ 4 & 3 & 2 & 1 & 0 & 8 & 7 & 6 & 5\\ 5 & 4 & 3 & 2 & 1 & 0 & 8 & 7 & 6\\ 6 & 5 & 4 & 3 & 2 & 1 & 0 & 8 & 7\\ 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & 8\\ 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 \end{bmatrix}}$

Hint: Use the outer() function and the %% operator.

8. Use R to calculate the following sums:

(a) $\displaystyle{\sum_{i=1}^{20} \sum_{j=1}^5 \frac{i^4}{(3+ij)}}$     (b) $\displaystyle{\sum_{i=1}^{10} \sum_{j=1}^i \frac{i^4}{(3+ij)}}$

(a) 89912.02;   (b) 6944.743