# Vectors, Matrices, and Some Things We Can Do With ThemPart II

## Matrices

A matrix (plural: matrices) is an array of numbers, arranged into rows and columns.
The size of a matrix tells us how many rows and how many columns it has: $$r \times c$$.

Example: Some $$2 \times 3$$ matrices:
$$\begin{bmatrix} 1 & 2 & 3\\4 & 5 & 6\end{bmatrix} \ \ \ \ \ \ \begin{bmatrix} -7 & 0 & 0.4\\ 0 & \pi & \sqrt{3}\end{bmatrix}$$

A square matrix is any matrix with an equal number of rows and columns.

Note that a vector is really just a matrix with one row or one column.

## Matrices

We usually name matrices with uppercase letters, like A, B, M, N, etc.
As with vectors, the numbers in a matrix are called entries or components.

We can refer to a particular entry by saying which row and column it's in: $$\mbox{name of matrix}[\mbox{row}][\mbox{column}]$$

Example: The matrix
$$A = \begin{bmatrix} 4 & 0 & -2\\6 & 8 & 1\end{bmatrix}$$ has entries $$A = 4 \ \ \ \ \ A = 0 \ \ \ \ \ A = -2$$ $$A = 6 \ \ \ \ \ A = 8 \ \ \ \ \ A = 1$$

## Some Special Matrices

• A zero matrix is a matrix where every entry is 0. It can be any size.

Example:
$$\begin{bmatrix} 0 & 0 & 0\\ 0 & 0 & 0\end{bmatrix} \ \ \ \ \ \begin{bmatrix} 0 & 0\\0 & 0\end{bmatrix} \ \ \ \ \ \begin{bmatrix} 0 \\0\end{bmatrix}$$

• An identity matrix is a square matrix where the entries on its diagonal are 1 and all other entries are 0. We call these matrices $$I_n$$, where $$n$$ is the number of rows and columns they have.

Example:
$$I_2 = \begin{bmatrix} 1 & 0\\0 & 1\end{bmatrix} \ \ \ \ I_3 = \begin{bmatrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1\end{bmatrix} \ \ \ \ I_4 = \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 &0\\ 0 & 0 & 1 &0\\0&0&0&1\end{bmatrix}$$

Just like with vectors, we can only add or subtract matrices with the same size.

To add two matrices together, just add their like components to make a new matrix of the same size:

Example:
\begin{align*}\begin{bmatrix} 4 & 0 & -2\\1 & 7 & 2\end{bmatrix} + \begin{bmatrix} 6 & 9 & 1\\-4 & 0 & 3\end{bmatrix} &= \begin{bmatrix} 4+6 & 0 + 9 & -2 + 1\\1-4 & 7 + 0 & 2 + 3\end{bmatrix}\\ &= \begin{bmatrix} 10 & 9 & -1\\-3 & 7 & 5\end{bmatrix} \end{align*}

Example:
$$\begin{bmatrix} 6\\9\end{bmatrix} + \begin{bmatrix} 3 & 0 \\1 & -2\end{bmatrix} \mbox{ is undefined (sizes unequal)}$$

## Matrix Multiplication

Unlike with vectors, we can multiply matrices. We'll focus on a special case: the product of a matrix and a vector.

There is only one rule:
The product of a matrix $$A$$ and a vector $$v$$ is only defined if the number of columns in $$A$$ is equal to the number of entries in $$v$$.

Example: Neither product below is defined.
$$\begin{bmatrix} 1 & 2 & 3\\4 & 5 & 6\end{bmatrix} \begin{bmatrix} 14 \\ 20 \end{bmatrix} \ \ \ \ \ \begin{bmatrix} 1 & 2 \\4 & 5 \end{bmatrix} \begin{bmatrix} 14 \\ 20 \\ 15 \end{bmatrix}$$

## Matrix Multiplication

To find the product of a matrix and a vector, we find the dot product of each row of the matrix with the vector. The $$n$$th dot product we find is the $$n$$ entry of the new vector we'll make:

Example:
$$\begin{bmatrix} \color{blue}{2} & \color{blue}{0} \\ \color{purple}{-1} & \color{purple}{4} \end{bmatrix} \begin{bmatrix} \color{orange}{3}\\\color{orange}{5} \end{bmatrix} = \begin{bmatrix} \color{blue}{[2,0]} \cdot \color{orange}{[3,5]} \\ \color{purple}{[-1,4]} \cdot \color{orange}{[3,5]} \end{bmatrix}= \begin{bmatrix} (2)(3) + (0)(5) \\ (-1)(3) + (4)(5) \end{bmatrix}= \begin{bmatrix} 6 \\ 17 \end{bmatrix}$$

Example:
\begin{align*} \begin{bmatrix} \color{blue}{4} & \color{blue}{-2} & \color{blue}{3} \\ \color{purple}{6} & \color{purple}{3} & \color{purple}{0} \end{bmatrix} \begin{bmatrix} \color{orange}{1}\\\color{orange}{2} \\\color{orange}{3} \end{bmatrix} &= \begin{bmatrix} \color{blue}{[4,-2,3]} \cdot \color{orange}{[1,2,3]} \\ \color{purple}{[6,3,0]} \cdot \color{orange}{[1,2,3]} \end{bmatrix}\\&= \begin{bmatrix} (4)(1)+(-2)(2)+(3)(3) \\ (6)(1)+(3)(2)+(0)(3) \end{bmatrix}= \begin{bmatrix} 9\\ 12\end{bmatrix} \end{align*}

## Matrix Multiplication

This isn't something we'll be doing, but we can also multiply two matrices. If the matrix on the left has $$n$$ columns, the matrix on the right has to have $$n$$ rows. We find the product with a lot of dot products again:

Example:
\small{\begin{align*} \begin{bmatrix} \color{blue}{4} & \color{blue}{-2} & \color{blue}{3} \\ \color{purple}{6} & \color{purple}{3} & \color{purple}{0} \end{bmatrix} \begin{bmatrix} \color{orange}{1} & \color{pink}{5}\\\color{orange}{2} & \color{pink}{0}\\\color{orange}{3} & \color{pink}{-1}\end{bmatrix} &= \begin{bmatrix} \color{blue}{[4,-2,3]} \cdot \color{orange}{[1,2,3]} & \color{blue}{[4,-2,3]} \cdot \color{pink}{[5,0,-1]} \\ \color{purple}{[6,3,0]} \cdot \color{orange}{[1,2,3]} & \color{purple}{[6,3,0]} \cdot \color{pink}{[5,0,-1]} \end{bmatrix}\\ &= \begin{bmatrix} (4)(1)+(-2)(2)+(3)(3) & (4)(5)+(-2)(0)+(3)(-1)\\ (6)(1)+(3)(2)+(0)(3) & (6)(5)+(3)(0)+(0)(-1)\end{bmatrix}\\ &= \begin{bmatrix} 9 & 17\\ 12& 30\end{bmatrix} \end{align*}}

## Coming Up: Linear Transformations

In the next couple of classes, we'll use matrices as functions.
You've seen functions in algebra classes before, like this one: $$f(x) = x^2 - 4$$ This function takes in a number that we call $$x$$ until we're ready to specify it. It then squares that number, and then subtracts four.

The result is a new number.

We can imagine the function as acting on the number $$x$$: it transforms it (in a predictable way) into a new number.

Our example function acts on the number 3, transforming it into the number 5: $$f(3)= 3^2 - 4 = 9 - 4 =5$$

## Matrices as Transformations

Just as functions $$f(x)$$ act on numbers $$x$$, matrices act on vectors by multiplication:
When we multiply a vector $$v$$ by a matrix $$A$$, the result is a new vector.
Sometimes, not much happens.

Example:
\scriptsize{\begin{align*} \begin{bmatrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix} 3 \\4 \\ -2\end{bmatrix} = \begin{bmatrix} 3\\ 4 \\ -2\end{bmatrix} \end{align*}}

But usually, the matrix will transform our vector into an entirely different one.

## 2 Dimensional Transformations

How we interpret these transformations depends on what our vectors and matrices represent. We'll start with some very common examples: our vectors will represent points in two dimensions, and our matrices will "move them around".

Example: Let $$v = [1,3]$$

If we multiply $$v$$ by the matrix $$A = \begin{bmatrix} 4 & 0 \\ 0 & 2 \end{bmatrix}$$ we get $$\begin{bmatrix} 4 & 0 \\ 0 & 2 \end{bmatrix}\begin{bmatrix} 1\\3 \end{bmatrix}=\begin{bmatrix} 4\\6\end{bmatrix}$$

## 2 Dimensional Transformations

When we think of every point in the plane as a vector, we can act on all of them at the same time by the same matrix. These actions distort everything in the plane in one of these ways:

• Stretch the plane horizontally, vertically, or both

• Rotate all points in the plane around the origin

• Reflect the plane across the $$x$$ or $$y$$ axis

• Shear the plane (makes it slant)

• Project everything in the plane onto a single line through the origin

You don't need to know which matrix does what, but you can try out some transformations
using this applet.

## Colors as Vectors

As we've seen, colors on a computer screen can be represented as an ordered list of three numbers, ranging from 0 to 255. The first number is how much red we want, the second is green, and the third is blue.

Anything that is an ordered list can be thought of as a vector (since that's all vectors are).
Example: We can represent the RGB color (90, 150, 200) as the vector

$$\begin{bmatrix} \color{red}{90} \\ \color{green}{150} \\ \color{blue}{200}\end{bmatrix}$$ which is a nice light blue:

## Colors as Vectors

Why would we do this?

• Every pixel in an image on a screen has an RGB color vector

• If we want to adjust the color of an image, we do so by changing each pixel

• Once we find a matrix that represents the adjustment we're looking for, we just have to multiply the vector of each pixel by the same matrix

• After that, every pixel is adjusted in the same way

This is the process we go through to adjust the color of an image, whether it's enhancing the green, toning down the blue, increasing contrast, brightening the entire picture, or adding a color filter such as sepia or black & white.

## Colors as Vectors

That's a lot of work! Fortunately, it's not much work for us, because we'll let a computer take care of it. But to appreciate what's happening, consider this:

• Most current smartphones have a 12 megapixel sensor, meaning they take images with 12 million pixels, each represented by a vector [R, G, B]

• To adjust an image, we'll need to multiply each one by a $$3 \times 3$$ matrix.

• To find just one of those matrix/vector products requires 9 dot products.

• Each dot product requires three products and two sums.

• That's 27 products and 18 sums per pixel.

• If we wanted to work all of this out by hand, we'd need to find:
• 324,000,000 products of two numbers, and
• 216,000,000 sums of two numbers

That's 540,000,000 operations every time we adjust the image!

## One Other Problem

The components of our color vector can only be between 0 and 255. But sometimes our matrix will cause a value in our new color vector to be outside this range. How we deal with that is up to us, but the simplest is to make everything over 255 equal to 255, and anything less than 0 equal to 0.

If the matrix has decimals in it, our new color vector will too, so we may also need to round.

$$\begin{bmatrix} 2 & 0 & 0 \\ 0 & 2.1 & 0 \\ 0 & 0 & -1 \end{bmatrix}\begin{bmatrix}200 \\ 50 \\ 10 \end{bmatrix} = \begin{bmatrix}\color{red}{400} \\ 100.5 \\ \color{red}{-1} \end{bmatrix}= \begin{bmatrix}255 \\ 101 \\ 0\end{bmatrix}$$

Some photo editors use other schemes for this problem, but this is not an unusual method of handling values outside the possible range.

## Basic Example

Before we get into a nice picture, we'll just see what a matrix does to a few colors. Below is a matrix, ready to multiply a vector.

When you change the matrix, you'll see how the colors are affected.

## A Better Example

We'll have to leave the slideshow for now: 3x3 color filtering matrices

## What's Left?

We've now seen how matrices effect vectors, representing changes in color, shape, and size.

While it's good to know matrices can do that, we now need to figure out how to find the matrix that will do it.

For example, we might know that matrices can be used to rotate a figure, but how to we find just the right matrix? In other words, how do we find the function we need to use to get the effect we want?

## A Review: Finding Functions

Before we go on: What's coming up on the next few slides is a review of some techniques you've seen in math classes before. You do not need to know these techniques for this course, but it's a reminder that you have found functions that satisfy certain requirements before.

Next week, we'll see how to find functions as matrices that help us do what we need to.

## A Review: Finding Linear Functions

Example: Find an equation of the line that passes through the point $$(2,3)$$ and has slope 5.
Since we know a point $$(x_1,y_1)$$ on our line, and we know the slope $$m$$, we can use the point slope form of the line: $$y - y_1 = m(x-x_1)$$ which gives us $$y - 3 = 5(x - 2)$$ This is a perfectly valid equation, but we can solve for $$y$$ if you prefer slope intercept form: $$y = 5x - 7$$

## A Review: Finding Quadratic Functions

Example: Find the equation of a parabola that has roots (x-intercepts) at $$x=3$$ and $$x=-1$$.
If the parabola has a root at $$x=3$$, then it must have a factor of $$(x-3)$$. Similarly, it must also have a factor of $$(x+1)$$. So our equation would be $$y = (x-3)(x+1) = x^2-2x-2$$ If we multiply our formula by any number, we'll still have a parabola with those roots, such as $$y = 5(x-3)(x+1) = 5x^2-10x-10$$ We'd need more information, like a point on the graph, to specify which parabola we want.

## Next Week

Example: Find a matrix that stretches an image horizontally by a factor of 3.
Example: Find a matrix that rotates an image by 90 degrees.
Example: Find a matrix that removes all shades of red from an image.