No history yet

Vectors and Matrices

What Are Vectors?

Think of a vector as a list of numbers that has a specific order. Each number in the list represents a feature or a coordinate. For example, if we're describing a house, a vector could be [2000, 3, 2.5], representing 2000 square feet, 3 bedrooms, and 2.5 bathrooms. In a 2D graph, a vector like [4, 2] is an arrow that points from the origin to the coordinate (4, 2). It has both a length (magnitude) and a direction.

Vectors are essentially organized lists of numbers that can represent data points or directions in space.

We can perform a few basic operations with vectors. Let's use two simple vectors, vector A=[4,2]A = [4, 2] and vector B=[1,3]B = [1, 3], to see how this works.

Vector Operations

Addition To add two vectors, you just add their corresponding elements. This creates a new vector. Geometrically, it's like placing the tail of the second vector at the head of the first one. The new vector goes from the start of the first to the end of the second.

A+B=[4+1,2+3]=[5,5]A + B = [4+1, 2+3] = [5, 5]

Subtraction Subtraction is similar. You just subtract the elements of the second vector from the first.

AB=[41,23]=[3,1]A - B = [4-1, 2-3] = [3, -1]

Scalar Multiplication A scalar is just a single number (not a vector). When you multiply a vector by a scalar, you multiply every element in the vector by that number. This scales the vector, making it longer or shorter. Multiplying by a negative scalar reverses its direction.

2A=[24,22]=[8,4]2A = [2 \cdot 4, 2 \cdot 2] = [8, 4]

Meet the Matrix

If a vector is a list of numbers, a matrix is a grid of numbers, arranged in rows and columns. You can think of a matrix as a collection of vectors stacked on top of each other. For instance, if we have data for three different houses, we can organize them into a matrix.

Square FeetBedroomsBathrooms
200032.5
150022.0
280043.0

This is a 3x3 matrix because it has 3 rows and 3 columns. Matrices are powerful because they let us organize and manipulate entire datasets at once.

Matrix Operations

Addition Just like with vectors, you can add two matrices as long as they have the same dimensions. You simply add the corresponding elements.

(1234)+(5678)=(1+52+63+74+8)=(681012)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} + \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix} = \begin{pmatrix} 1+5 & 2+6 \\ 3+7 & 4+8 \end{pmatrix} = \begin{pmatrix} 6 & 8 \\ 10 & 12 \end{pmatrix}

Transposition Transposing a matrix means flipping it over its main diagonal. The rows become columns and the columns become rows. We denote the transpose of a matrix AA as ATA^T.

A=(123456)AT=(142536)A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} \quad A^T = \begin{pmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{pmatrix}

Multiplication Matrix multiplication is a bit more involved. To multiply two matrices, the number of columns in the first matrix must equal the number of rows in the second. You multiply the elements of each row of the first matrix by the elements of each column of the second matrix and sum the products.

Here’s how to find the top-left element of the result: take the first row of the first matrix and the first column of the second, multiply them element by element, and add the results.

(1234)×(5678)=((15+27)(16+28)(35+47)(36+48))\begin{pmatrix} \mathbf{1} & \mathbf{2} \\ 3 & 4 \end{pmatrix} \times \begin{pmatrix} \mathbf{5} & 6 \\ \mathbf{7} & 8 \end{pmatrix} = \begin{pmatrix} (\mathbf{1} \cdot \mathbf{5} + \mathbf{2} \cdot \mathbf{7}) & (1 \cdot 6 + 2 \cdot 8) \\ (3 \cdot 5 + 4 \cdot 7) & (3 \cdot 6 + 4 \cdot 8) \end{pmatrix}
=(19224350)= \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}

This process of multiplying rows by columns is fundamental to how machine learning models process data and make predictions.

Vectors and matrices are the language of data for computers. An image can be a matrix of pixel values. A user's preferences can be a vector of ratings. Machine learning algorithms use the operations we've discussed to find patterns, make classifications, and power predictions based on this numerical data.

Quiz Questions 1/6

What is the result of multiplying the vector [3,1,5][3, -1, 5] by the scalar 4?

Quiz Questions 2/6

Which of the following operations is NOT possible?

By understanding these basic building blocks, you're taking a big step toward understanding how AI and machine learning models actually work.