# Matrices and Matrix Operations

Computer Science I · Algebra · https://tryals.app/learn/computer-science-i/matrices-and-matrix-operations

## Matrices and Basic Operations

A **matrix** is a rectangular array of numbers arranged in **rows** and **columns**. An $m \times n$ matrix has $m$ rows and $n$ columns. They compactly represent linear systems, transformations, and data tables.

| Operation | Rule | Shape Requirement |
| --- | --- | --- |
| **Addition** | Add entry by entry | Same shape ($m \times n$) |
| **Scalar Multiplication** | Multiply every entry by a scalar | Any shape |

The **identity matrix** $I$ has ones on the diagonal and zeros elsewhere, acting like the number 1 so that $AI = A$. A square matrix may have an **inverse** $A^{-1}$ where $AA^{-1} = I$, though not all matrices are invertible.

## Matrix Multiplication & Transformations

The product $AB$ is defined only when the number of **columns of $A$** equals the number of **rows of $B$**. An $m \times n$ times an $n \times p$ matrix yields an $m \times p$ result. Entry $(i, j)$ is the **dot product** of row $i$ of $A$ and column $j$ of $B$.

Matrix multiplication is **not commutative**: in general $AB \neq BA$, though it is associative $A(BC) = (AB)C$. This odd rule matters because a matrix represents a linear transformation, and multiplying matrices **composes** those transformations in sequence.

> **Common pitfall:** Never multiply matrices entry-by-entry. Always use the **dot product of a row and a column**, and verify that shapes match.

## Practice questions

5 of this lesson's 9 practice questions, with answers. The full set is in the app.

### 1. Graphics uses 4×4 matrices for a three-dimensional world. What does the extra dimension make possible?

A. Higher numerical precision in the transformation pipeline
B. Storing colour alongside position in a single matrix
C. Representing time as a fourth axis alongside the three spatial ones
D. Translation as a matrix, since shifting the origin is not linear

**Answer:** D. Translation as a matrix, since shifting the origin is not linear

**Why:** A linear map fixes the origin and translation does not, so no 3×3 matrix can translate. Embedding points as (x, y, z, 1) makes it a shear in four dimensions, linear again, so the whole pipeline becomes matrix multiplication. Perspective division comes free from the same trick.

Page: https://tryals.app/practice/computer-science-i/matrices-and-matrix-operations/graphics-uses-4-4-matrices-for-a-three-dimensional-world-what-does

### 2. Order the steps to compute entry (2,1) of AB, where A has rows (1,2),(3,4) and B has rows (5,6),(7,8).

**Answer:**

1. Take row 2 of A: (3, 4)
2. Take column 1 of B: (5, 7)
3. Form the dot product 3*5 + 4*7
4. Get 15 + 28 = 43

**Why:** Entry (2,1) uses A's row 2 and B's column 1: (3,4) dot (5,7) = 15 + 28 = 43. The row index picks the left row, the column index picks the right column.

Page: https://tryals.app/practice/computer-science-i/matrices-and-matrix-operations/order-the-steps-to-compute-entry-2-1-of-ab-where-a-has-rows

### 3. Sort each matrix operation by whether the two matrices must have identical shape.

**Answer:**

- Needs identical shape: Addition A + B, Subtraction A - B
- Does not need identical shape: Multiplication A times B, Scalar multiple cA

**Why:** Addition and subtraction act entry by entry, so shapes must match exactly. Multiplication needs only columns-of-A = rows-of-B, and a scalar multiple involves a single matrix, neither needs identical shapes.

Page: https://tryals.app/practice/computer-science-i/matrices-and-matrix-operations/sort-each-matrix-operation-by-whether-the-two-matrices-must-have

### 4. Let A be 3x2 and B be 2x3. Select every statement that is TRUE.

A. BA is defined
B. AB is defined
C. AB is 3x3
D. A + B is defined
E. BA is 2x2

**Answer:** A. BA is defined; B. AB is defined; C. AB is 3x3; E. BA is 2x2

**Why:** AB has inner dimensions 2 = 2 and result 3x3; BA has inner 3 = 3 and result 2x2, both defined. A + B needs identical shapes, but 3x2 and 2x3 differ, so the sum is undefined.

Page: https://tryals.app/practice/computer-science-i/matrices-and-matrix-operations/let-a-be-3x2-and-b-be-2x3-select-every-statement-that-is-true

### 5. Graphics libraries compose a rotation and a translation by multiplying their matrices, then apply the single product to every vertex. Why is that faster than applying the two transformations in turn?

A. Because the product is computed once and applied to every vertex.
B. Because matrix multiplication is faster than matrix-vector multiplication.
C. Because the GPU can only accept one matrix at a time.
D. Because applying two transformations in turn gives a different result from applying their product.

**Answer:** A. Because the product is computed once and applied to every vertex.

**Why:** Two transformations applied separately cost two matrix-vector multiplies per vertex, so a million vertices cost two million; composing first costs one small matrix-matrix product, once. That is why the model-view-projection matrix exists. The results are identical because multiplication *is* composition, what matters is order, which is the source of a great many graphics bugs.

Page: https://tryals.app/practice/computer-science-i/matrices-and-matrix-operations/graphics-libraries-compose-a-rotation-and-a-translation-by
