Search

Search papers and lessons

Vector

Level 1 · 9 min read

A vector is an ordered list of numbers that you can read as an arrow or as a point, and you can add vectors, scale them and measure their length.

On this page

TL;DR

A vector is a list of numbers, such as (3,4)(3, 4). You can read the list as a point on a grid, or as an arrow from the origin to that point. Vectors can be added, stretched and shrunk, and measured for length, and those operations are the basic arithmetic of machine learning.

Where you'll see it: a data point with three measurements is a 3-dimensional vector, a colour is a vector of red, green and blue values, and a neural network layer takes a vector in and returns a vector out.

Intuition

A list that also has a direction

Suppose you walk 3 blocks east and 4 blocks north. You can describe that move with two numbers, (3,4)(3, 4): how far you went east and how far you went north. The pair records both how far and which way, and it does not care where you started. Walking 3 east and 4 north from home or from the library gives the same instruction.

That pair of numbers is a vector. Drawn as an arrow from the origin (0,0)(0, 0) to the point (3,4)(3, 4), it has a length of 5 blocks and points north-east. Drawn as a point, it is just a location on the grid. Both pictures describe the same object, and which one helps depends on the problem.

The reason to bundle numbers into one object is that most real problems have many numbers that belong together. A house has a size, a number of bedrooms and an age. A pixel has a red, a green and a blue value. A step of gradient descent moves every weight of a model at once. Writing each group as one vector lets you name the whole group with one symbol and apply one rule to every part at once.

−6−4−20246−6−4−20246first componentsecond component
  • points at length 5
Every point on this circle is a vector of length 5 from the origin. The markers (3, 4), (4, 3) and (-5, 0) are three of them.

Where vectors fit in the bigger picture

The idea shows up in three roles, and the rest of this graph builds on each of them. A vector can be a list of measurements, a point in space, or an arrow with a direction and a length. Stacking several vectors side by side gives a Matrix, and a matrix is the machinery that transforms many vectors at once. Two vectors can be combined by a dot product to measure how much they point the same way. And a Gradient is itself a vector, one entry per input variable, pointing in the direction in which a function grows fastest.

flowchart LR
  A["Vector: an ordered list of numbers"] --> B["Matrix: a stack of vectors"]
  A --> C["Dot product: how much two vectors agree"]
  A --> D["Gradient: a vector of slopes"]
  B --> E["Linear maps and neural network layers"]
  D --> F["Gradient descent"]

The math

Definition

Definition: Vector

A vector of dimension nn is an ordered list of nn real numbers, written as a column

v=(v1v2⋮vn)∈Rn.\mathbf{v} = \begin{pmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{pmatrix} \in \mathbb{R}^n.

Here Rn\mathbb{R}^n is the set of all such lists, and viv_i is the ii-th component, the number in position ii. The order matters: (3,4)(3, 4) and (4,3)(4, 3) are different vectors. We write vectors in bold, such as v\mathbf{v}, to tell them apart from single numbers, which are called scalars. Writing the list as a row, (v1,…,vn)(v_1, \dots, v_n), is a common shorthand, and the column form is what you need once matrices appear.

Adding vectors and scaling them

Two vectors of the same dimension are added component by component:

u+v=(u1+v1⋮un+vn).\mathbf{u} + \mathbf{v} = \begin{pmatrix} u_1 + v_1 \\ \vdots \\ u_n + v_n \end{pmatrix}.

Multiplying a vector by a scalar cc multiplies every component by cc:

c v=(c v1⋮c vn).c\,\mathbf{v} = \begin{pmatrix} c\,v_1 \\ \vdots \\ c\,v_n \end{pmatrix}.

Geometrically, addition is "walk along one arrow, then along the other", and scaling by cc stretches the arrow by a factor ∣c∣|c|, flipping it if c<0c \lt 0. Subtraction is addition of a negative: u−v=u+(−1)v\mathbf{u} - \mathbf{v} = \mathbf{u} + (-1)\mathbf{v}.

Length

The length, or norm, of a vector is the square root of the sum of its squared components:

∥v∥=v12+v22+⋯+vn2.\lVert \mathbf{v} \rVert = \sqrt{v_1^2 + v_2^2 + \cdots + v_n^2}.

In two dimensions this is Pythagoras' theorem: the arrow is the hypotenuse of a right triangle whose legs are v1v_1 and v2v_2. The same formula works in any dimension, even when there is no picture to draw.

A vector of length 1 is a unit vector. Any nonzero vector can be turned into one by dividing by its length, v^=v/∥v∥\hat{\mathbf{v}} = \mathbf{v} / \lVert \mathbf{v} \rVert. This keeps the direction and throws away the size, which is often exactly what you want.

Distance between points

If two points are described by vectors a\mathbf{a} and b\mathbf{b}, the vector from one to the other is b−a\mathbf{b} - \mathbf{a}, and the distance between them is its length, ∥b−a∥\lVert \mathbf{b} - \mathbf{a} \rVert. Subtracting first and then measuring is the basic move behind nearest-neighbour search and most error measures.

Linear combinations

A linear combination of vectors v1,…,vk\mathbf{v}_1, \dots, \mathbf{v}_k is any sum c1v1+⋯+ckvkc_1 \mathbf{v}_1 + \cdots + c_k \mathbf{v}_k with scalar weights cic_i. The set of all such combinations is the span of those vectors. A matrix times a vector is a linear combination of the matrix's columns, so this operation reappears throughout linear algebra.

Worked examples

Example
Adding, scaling and subtracting

Let u=(1,2)\mathbf{u} = (1, 2) and v=(3,−1)\mathbf{v} = (3, -1).

  1. Addition: u+v=(1+3, 2+(−1))=(4,1)\mathbf{u} + \mathbf{v} = (1 + 3,\ 2 + (-1)) = (4, 1).
  2. Scaling: 2u=(2⋅1, 2⋅2)=(2,4)2\mathbf{u} = (2 \cdot 1,\ 2 \cdot 2) = (2, 4).
  3. Subtraction: u−v=(1−3, 2−(−1))=(−2,3)\mathbf{u} - \mathbf{v} = (1 - 3,\ 2 - (-1)) = (-2, 3).
  4. Check the geometry: u+v\mathbf{u} + \mathbf{v} lands at (4,1)(4, 1), which is where you end up after walking along u\mathbf{u} and then along v\mathbf{v}.
Example
Length and a unit vector

Let v=(3,4)\mathbf{v} = (3, 4).

  1. Squared components: 32=93^2 = 9 and 42=164^2 = 16.
  2. Sum: 9+16=259 + 16 = 25.
  3. Square root: ∥v∥=25=5\lVert \mathbf{v} \rVert = \sqrt{25} = 5.
  4. Unit vector: v^=(3/5, 4/5)=(0.6, 0.8)\hat{\mathbf{v}} = (3/5,\ 4/5) = (0.6,\ 0.8). Its squared components add to 0.36+0.64=10.36 + 0.64 = 1, so its length is 1, as it should be.
  5. Interpretation: the arrow for (3,4)(3, 4) is 5 units long, and (0.6,0.8)(0.6, 0.8) points the same way with length 1.
Example
Distance between two points in 3D

A drone is at a=(1,2,2)\mathbf{a} = (1, 2, 2) and a sensor is at b=(4,6,2)\mathbf{b} = (4, 6, 2), both in metres.

  1. Vector from the drone to the sensor: b−a=(3,4,0)\mathbf{b} - \mathbf{a} = (3, 4, 0).
  2. Length: 32+42+02=25=5\sqrt{3^2 + 4^2 + 0^2} = \sqrt{25} = 5.
  3. Interpretation: the sensor is 5 metres away. The third component is zero because both points are at the same height.
Example
Averaging two colours

A pixel can be stored as an RGB vector, with each channel from 0 to 255. Take c1=(200,100,50)\mathbf{c}_1 = (200, 100, 50) and c2=(60,140,90)\mathbf{c}_2 = (60, 140, 90).

  1. Add them: (260,240,140)(260, 240, 140).
  2. Divide by 2, which is the same as multiplying by 12\tfrac{1}{2}: (130,120,70)(130, 120, 70).
  3. Interpretation: the midpoint colour is a brownish tone. Averaging is just a scaled sum applied to each component, so one line of vector arithmetic blends the whole pixel at once.

Tradeoffs and failure modes

Vectors are the right tool whenever a group of numbers should be handled as one object. They stop helping in a few predictable ways.

SituationWhat goes wrongBetter choice
Components have different units (metres and kilograms)Length adds metres to kilograms, and the answer depends on the unit you pickedRescale each component first, for example by standardising
Adding vectors of different sizesComponent-wise addition is not definedPad, project or use a different representation
Normalising a zero vectorDivision by ∥0∥=0\lVert \mathbf{0} \rVert = 0Check for zero, or add a small constant
Confusing row and column shapesMatrix products fail or quietly give the wrong shapeWrite the shape next to every symbol
Treating vectors as arrows onlyMisses data with many dimensions, where no picture existsTrust the algebra and the length formula

Also, a list of numbers is only a vector if the operations make sense for it. A list of product names is not a vector, even though you can write it down.

History

Arrow-like quantities were used in mechanics for centuries, for forces and velocities, long before anyone wrote them as lists of numbers. The modern idea grew out of 19th-century work on complex numbers and geometry. In 1843 William Rowan Hamilton introduced quaternions, which are four-component numbers with a vector part, while Hermann Grassmann published a general theory of extensions in 1844 that anticipated vector spaces.

Josiah Willard Gibbs and Oliver Heaviside then reshaped these ideas into the three-dimensional vector calculus that physics textbooks still use, in the late 1800s. Giuseppe Peano gave the first axiomatic definition of a vector space in 1888. The shift to lists of numbers caught on because it turned geometry into arithmetic that a computer could carry out, and let the same tools apply to data, signals and, later, neural networks.

Check yourself

QuestionCompute u+3v\mathbf{u} + 3\mathbf{v} when u=(1,−2)\mathbf{u} = (1, -2) and v=(2,5)\mathbf{v} = (2, 5).Show answer

Scale first: 3v=(6,15)3\mathbf{v} = (6, 15). Then add component by component: u+3v=(1+6, −2+15)=(7,13)\mathbf{u} + 3\mathbf{v} = (1 + 6,\ -2 + 15) = (7, 13).

QuestionWhat is the length of v=(5,12)\mathbf{v} = (5, 12), and what is its unit vector?Show answer

The length is 52+122=169=13\sqrt{5^2 + 12^2} = \sqrt{169} = 13. The unit vector is (5/13, 12/13)(5/13,\ 12/13), which has length 1 and points in the same direction as v\mathbf{v}.

QuestionWhy does the order of the components matter, and give an example where swapping them changes the meaning.Show answer

A vector is an ordered list, so position carries meaning. Swapping components changes which coordinate is which. For example, (170,65)(170, 65) as (height in cm, weight in kg) is a person, while (65,170)(65, 170) is a different and nonsensical input to the same model.

QuestionA model takes a vector of features measured in metres and kilograms. A student computes the distance between two data points and gets a very large number that is dominated by the weight. What went wrong, and what would you do?Show answer

The Euclidean distance adds squared differences across components regardless of units, so a feature with large numbers (kilograms here) dominates. The fix is to standardise each feature, for example by subtracting its mean and dividing by its standard deviation, so every component is on a comparable scale before measuring distance.

Sources