Why This Matters
Every time you scroll a map, apply a photo filter, or train an AI model, math is doing the heavy lifting behind the scenes. The two workhorses of that math are vectors and matrices. A vector is just an ordered list of numbers -- something you already know as an array. A matrix is a 2D grid of numbers -- an array of arrays.
Once you see vectors and matrices as data structures with rules, the mystery disappears. You can add them, scale them, and multiply them to transform data, rotate 3D objects, solve systems of equations, and train neural networks. Linear algebra is the language that computers speak when they do anything visual or intelligent.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Vectors and matrices are arrays with rules for addition, scaling, and multiplication.
Code Example
// --- Vectors as arrays ---
const v1 = [1, 2, 3];
const v2 = [4, 5, 6];
// Vector addition: component by component
const add = v1.map((val, i) => val + v2[i]);
console.log("add:", add); // [5, 7, 9]
// Scalar multiplication
const scaled = v1.map(val => val * 3);
console.log("scaled:", scaled); // [3, 6, 9]
// --- Matrices as 2D arrays ---
const A = [[1, 2], [3, 4]];
const B = [[5, 6], [7, 8]];
// Matrix addition
const matAdd = A.map((row, i) =>
row.map((val, j) => val + B[i][j])
);
console.log("matAdd:", matAdd); // [[6,8],[10,12]]
// Matrix multiplication (2x2)
function matMul(A, B) {
const rows = A.length;
const cols = B[0].length;
const n = B.length;
const result = [];
for (let i = 0; i < rows; i++) {
result[i] = [];
for (let j = 0; j < cols; j++) {
let sum = 0;
for (let k = 0; k < n; k++) {
sum += A[i][k] * B[k][j];
}
result[i][j] = sum;
}
}
return result;
}
console.log("matMul:", matMul(A, B)); // [[19,22],[43,50]]
// Identity matrix: A * I = A
const I = [[1, 0], [0, 1]];
console.log("A * I:", matMul(A, I)); // [[1,2],[3,4]]
// Transpose: swap rows and columns
function transpose(M) {
return M[0].map((_, j) => M.map(row => row[j]));
}
console.log("transpose:", transpose(A)); // [[1,3],[2,4]]Interactive Experiment
Try these exercises:
- Create two 3-element vectors and add them. Verify each component was added separately.
- Multiply a vector by 0. What happens? Multiply by -1. What happens?
- Create a 2x3 matrix and a 3x2 matrix. Multiply them. What is the shape of the result?
- Multiply a matrix by the identity matrix. Does the original matrix change?
- Transpose a 2x3 matrix. What shape do you get?
Quick Quiz
Coding Challenge
Write a function called `transpose` that takes a 2D array (matrix) and returns its transpose. The transpose swaps rows and columns: element at row i, column j moves to row j, column i. For example, [[1, 2, 3], [4, 5, 6]] becomes [[1, 4], [2, 5], [3, 6]].
Real-World Usage
Vectors and matrices are everywhere in production software:
- Machine learning: Neural networks are chains of matrix multiplications. Training involves multiplying weight matrices by input vectors millions of times.
- Computer graphics: Every rotation, scaling, and projection in 3D games is a matrix multiplication. GPU hardware is optimized specifically for this.
- Recommendation systems: Users and products are represented as vectors. Finding similar items means comparing vectors.
- Image processing: An image is a matrix of pixel values. Filters like blur and sharpen are matrix operations (convolutions).
- Search engines: Document embeddings are high-dimensional vectors used to find relevant results.