Vectors in n dimensions: a list of many numbers

When people say "vector", most of us picture an arrow on a flat plane. That is just the example that is easiest to draw. Raise the dimension slider below. The picture changes, but the vector is still a list of numbers, and the rules for calculating with it stay the same.

Short answer. Before it is an arrow, a vector is an ordered list of numbers. With 2 numbers you can draw it as an arrow on a plane, and with 3 as an arrow in space. From 4 numbers on you cannot draw it, but the rules are exactly the same: add position by position, multiply every number by the same number, and multiply position by position and add up. The vectors used in AI usually have hundreds to thousands of numbers.

Bar chart: one bar per position. Drag the bars up and down.

Move the dimension slider from 1 to 8. Drag either picture or type in the number boxes, and both pictures change together. Each number goes from −5 to 5.

Definitions

Here are the textbook definitions of the words used on this page. a, b and v are vectors in ℝⁿ.

vector in ℝⁿ
An ordered list of n real numbers, v = (v₁, v₂, …, vₙ), is an n-dimensional vector, and vᵢ is its i-th component. ℝⁿ is the set of all such vectors. Two vectors are equal when all components in the same position are equal.

ℝn={(v1,v2,…,vn)∣v1,v2,…,vn∈ℝ}

dimension
The number of vectors in a basis of a vector space (a group of vectors that is linearly independent and spans the space). Every basis of a space has the same number of vectors. ℝⁿ has the standard basis e₁, …, eₙ, where only one position is 1, so its dimension is n.

dimℝn=n,𝐞1=(1,0,…,0),…,𝐞n=(0,…,0,1)

norm
The positive square root of the sum of the squares of the components. It is also called the Euclidean norm.

‖𝐯‖=∑i=1nvi2=v12+v22+⋯+vn2

Euclidean distance
The norm of the difference of two vectors.

d(𝐚,𝐛)=‖𝐚−𝐛‖=∑i=1n(ai−bi)2

dot product
The real number you get by multiplying the components in the same position and adding them all. Seen as column vectors, it equals the matrix product aᵀb.

𝐚·𝐛=∑i=1naibi=𝐚𝖳𝐛

cosine similarity
When a ≠ 0 and b ≠ 0, the dot product divided by the product of the two norms. It is between −1 and 1. In n dimensions, the angle θ between two vectors is defined as the angle from 0° to 180° whose cos θ equals this value.

cosθ=𝐚·𝐛‖𝐚‖‖𝐛‖

column vector, row vector
A vector written as an n×1 matrix is a column vector, and one written as a 1×n matrix is a row vector. Transposing a column vector gives a row vector.

𝐯=[v1v2⋮vn],𝐯𝖳=[v1v2⋯vn]

1. A vector is an ordered list of numbers

A vector is a few numbers kept in a fixed order. (3, 4) is a vector with 2 numbers, and (3, 4, 2, −1) is a vector with 4 numbers. The order matters, so (3, 4) and (4, 3) are different vectors. Each number in the list is called a component, and how many numbers there are (n) is called the dimension. For a programmer, it is an array of numbers with a fixed length.

You can look at the same vector in three ways. All three are the same thing seen differently.

Way of seeingHow you see (3, 4)When it helps
List of numbers3 in the first position, 4 in the secondAlways works. This is how a computer stores it (an array)
PointThe spot (3, 4) on a planeWhen one record is one point in space. Close points = similar records
ArrowAn arrow from the origin to (3, 4)When you care about direction and length. Adding = walking one arrow after another (Vectors)

Points and arrows can only be drawn for 2 or 3 numbers. A list works for any number of numbers. So the list is what a vector really is, and the arrow is a picture you use when you can draw one. That is why the two pictures in the experiment always move together.

How to write it

Try this. 1) The starting value is v = (3, 4). The arrow and the two bars are the same vector, and the length is √(3² + 4²) = 5. Drag bar v2 and the arrow tip moves up and down. 2) Raise the dimension to 3, and v = (3, 4, 2) becomes an arrow in space. Its length is 5.39. Drag the picture sideways to turn it. 3) At 4, the arrow disappears and a parallel coordinates plot appears: v = (3, 4, 2, −1), length 5.48. At 8, the length is 6.93. 4) Go down to 1, and a single point (3) on a number line is left. One number is also a vector, a 1-dimensional one.

2. Dimension means "how many numbers"

We learn that 2D is a plane and 3D is space, so it is natural to think of dimension as "how many directions space has". For vectors it is simpler. The dimension is the number of numbers. The numbers do not have to be directions in space. The color of one pixel has nothing to do with space, but it is 3 numbers (red, green, blue), so it is a 3-dimensional vector.

WhatThe list of numbersDimension
The color of one pixelHow strong red, green and blue are. Example: (255, 128, 0)3
One movie on this page (section 7)Scores for (action, comedy, romance, horror, music)5
The state of one flying ballPosition (x, y, z) followed by velocity (x, y, z)6
One 28 × 28 grayscale imageOne brightness per pixel, written out in one long line. The handwritten digit collection MNIST uses this size784
A word or sentence embeddingThe numbers a model outputsHundreds to thousands (depends on the model)

The strict definition in textbooks is a little different. The dimension is "the smallest number of base vectors you can mix to make every vector in the space" (Basis and change of coordinates, Linear combinations and span). Every vector in ℝn can be made from the n base vectors (1, 0, …, 0), (0, 1, …, 0), and so on, with a single 1 each, and fewer is not enough, so its dimension is n. That is why in ℝn "the number of numbers" and "the dimension" are the same. But if your data only sits on a thin slice inside it (say, a flat surface inside 784 dimensions), the data really uses fewer directions. Finding those directions is what principal component analysis (PCA) does.

3. How to look at vectors you cannot draw

With 1, 2 or 3 numbers you can draw on a number line, a plane or in space. The space we live in has only three directions that are all at right angles to each other, so there is nowhere to put a fourth number. That is why from 4 dimensions on you cannot draw an arrow. You can still look at the vector, though.

Try this. Set the dimension to 4 and drag the fourth axis (v4) of the parallel coordinates plot upward. The end of the bent line and bar v4 go up together. The two pictures draw the same four numbers in different ways.

4. The rules stay the same

Every calculation you learned in 2D was done "once per position". So it works with any number of positions. Change n below and pick a calculation. The two vectors a and b always have the same dimension.

CalculationFormula (for each position i)What you get
Adda + b = (a1 + b1, …, an + bn)A vector (n numbers)
Scale (multiply by one number)k a = (k × a1, …, k × an)A vector (n numbers)
Dot producta · b = Σ ai × biOne number
Length‖a‖ = √(Σ ai²)One number (0 or more)
Distance‖a − b‖ = √(Σ (ai − bi)²)One number (0 or more)
Cosine similarity(a · b) ÷ (‖a‖ × ‖b‖)One number (−1 to 1)

If you have never seen Σ. Σ (sigma) means "let i go from 1 to n, and add up everything written after it". Books write i = 1 below the Σ and n above it in small type. For a programmer, it is one loop. Length adds ai × ai and then takes the square root, and distance squares ai − bi, adds, and takes the square root. They all have the same shape.

// a · b = Σ aᵢ × bᵢ (i = 1 … n) is this loop
let sum = 0;
for (let i = 0; i < n; i++) {   // math counts from 1, arrays from 0: a₁ is a[0]
  sum += a[i] * b[i];
}
// length ‖a‖ = √(Σ aᵢ²): add a[i] * a[i] in the same loop, then Math.sqrt(sum)

What the dot product and cosine similarity measure is on the Dot product page, and adding and scaling with 2D arrows is on the Vectors page. Multiplying several vectors by numbers and adding them is a linear combination. All of it works the same in any dimension.

Try this. 1) At the start (n = 4, Add), a = (1, 2, 2, 4), b = (2, 1, 0, 2), and a + b = (3, 3, 2, 6). 2) Press "Scale" to get 1.5 × a = (1.5, 3, 3, 6). Every bar grows by the same factor, and the length also grows 1.5 times, from 5 to 7.5. Set k to −1 and every bar flips. 3) Press "Dot product". The products 2, 2, 0, 8 are laid end to end and stop at 12. The cosine similarity is 0.8. 4) Lower n to 2. Now a = (1, 2), b = (2, 1), and a plane picture appears as well. Adding gives (3, 3) both with bars and with arrows. The dot product is 1×2 + 2×1 = 4 with bars, and measuring ‖a‖ × ‖b‖ × cos θ on the plane also gives 4 (θ = 36.87°).

5. Work it out by hand: two 4D vectors

a = (1, 2, 2, 4), b = (2, 1, 0, 2). These are the starting values of the experiment. You cannot draw 4 dimensions, but you can calculate position by position.

CalculationPosition by positionResult
a + b(1 + 2, 2 + 1, 2 + 0, 4 + 2)(3, 3, 2, 6)
1.5 × a(1.5×1, 1.5×2, 1.5×2, 1.5×4)(1.5, 3, 3, 6)
a · b1×2 + 2×1 + 2×0 + 4×2 = 2 + 2 + 0 + 812
‖a‖√(1² + 2² + 2² + 4²) = √(1 + 4 + 4 + 16) = √255
‖b‖√(2² + 1² + 0² + 2²) = √(4 + 1 + 0 + 4) = √93
a − b(1 − 2, 2 − 1, 2 − 0, 4 − 2)(−1, 1, 2, 2)
Distance ‖a − b‖√((−1)² + 1² + 2² + 2²) = √(1 + 1 + 4 + 4) = √10about 3.16
Cosine similarity12 ÷ (5 × 3) = 12 ÷ 150.8

Written as equations, the same work looks like this.

𝐚+𝐛=[1224]+[2102]=[3326],1.5𝐚=1.5[1224]=[1.5336]

𝐚·𝐛=∑i=14aibi=1·2+2·1+2·0+4·2=12

‖𝐚‖=12+22+22+42=25=5,‖𝐛‖=22+12+02+22=9=3

d(𝐚,𝐛)=‖𝐚−𝐛‖=(−1)2+12+22+22=10≈3.16

𝐚·𝐛‖𝐚‖‖𝐛‖=125·3=1215=0.8

In equations, a = (1, 2, 2, 4) is written standing up, as a column (a column vector, section 8). It is the same vector.

A cosine similarity of 0.8 means the two vectors point fairly similar ways. In 2D, that would be two arrows about 36.87° apart. In 4 dimensions, "angle" is defined through cosine similarity in exactly this way.

6. How the formula is built: length and distance

The length formula √(Σ ai²) is not something to memorize. It comes from using Pythagoras' theorem again and again. Step through it and watch one piece at a time join the inside of the square root. The pictures use v = (3, 4, 2, −1) from experiment 1 and a, b from experiment 2.

‖𝐯‖=x2+y2

32+42=9+16=25=5

The arrow (3, 4) on a plane is the long side of a right triangle with sides 3 and 4. Pythagoras' theorem (long side² = the sum of the squares of the other two sides) gives the length right away.

This straight-line distance is called the Euclidean distance. Because the gaps are squared, the sign disappears (−1 and 1 are equally far away), and big gaps count much more. A gap of 2 adds 4, and a gap of 7 adds 49. That is why, in the movie table in section 7, one big gap can almost decide the whole distance.

7. One row of data = one vector

One row of a table is a list of numbers, so it is a vector. Below is a made-up table of 6 movies with 5 features scored from 0 to 10. One movie is a 5-dimensional vector, and the whole table is a matrix with 6 rows and 5 columns. Pick two rows.

A small toy model: 6 made-up movies, 5 features. Real recommendation and search use hundreds of numbers or more, and a model sets them, not a person. So the single numbers do not have names like "action".

"Find movies with a similar taste" becomes "find nearby vectors". Text and images work the same way. An embedding model turns text into hundreds to thousands of numbers, and vector search finds the nearby ones. The dimension is bigger, but the job is the same as in this table.

Try this. 1) At the start (Movie A and Movie B), the distance is 2.65 and the cosine similarity is 0.97. Both are action movies. The biggest difference is music (gap −2, 57% of distance²). 2) Change the second movie to Movie C. The distance becomes 11.22 and the cosine similarity 0.46. The biggest gap is action (−7), which makes up 39% of distance². 3) Change the second movie to Movie F. The distance is 5.66, farther than B, but the cosine similarity is 0.99, higher than B. F points the same way as A, but all its scores are lower. Distance also looks at size, while cosine similarity only looks at direction. Which one to use is covered on the Vector search page.

8. Column vectors, row vectors, and m×n matrices

Write a list of numbers standing up and it is a column vector. Lay it on its side and it is a row vector. As matrices, a column vector is n×1 (n rows, 1 column) and a row vector is 1×n (1 row, n columns).

𝐚=[1224](4×1),𝐚𝖳=[1224](1×4)

As lists of numbers, they are the same. We tell them apart because in matrix multiplication the shapes have to fit. Books usually write vectors as columns, and to lay one on its side they add a small T (transpose) and write aT. You can even write the dot product as a matrix product: aT b is (1×n) × (n×1) = 1×1, which is one number.

An m×n matrix is a table that holds several vectors. Read it by rows and it is m vectors of n numbers stacked up. Read it by columns and it is n vectors of m numbers placed side by side. The movie table in section 7 is a 6×5 matrix. One row is one movie (a 5-dimensional vector), and one column is one feature (a 6-dimensional vector, for example the action scores of the 6 movies). Matrices themselves are covered on the Matrix basics page.

9. High dimensions do not behave like you expect

The rules do not change, but what feels true in 2D and 3D is sometimes wrong in high dimensions. The experiment below picks two random directions (vectors of length 1) in n dimensions, measures their cosine similarity, does this 2,000 times, and draws where the values pile up. It uses a fixed seed, so you always get the same picture.

At n = 2 the values spread from −1 to 1, and there are actually more of them near the two ends. As n grows, the values crowd into a narrow pile around 0. The width of the pile is about 1/√n. In other words, in high dimensions almost any two directions are close to perpendicular. The dot product Σ ui × vi is a sum of n small products, and when the directions are random, the signs of those products are random too, so they cancel each other out.

So the same number weighs differently in different dimensions. A cosine similarity of 0.3 is common in 2D, where 39% of random pairs go above it. In 200 dimensions, none of the 2,000 random pairs reaches it. If you see 0.3 in high dimensions, it may be a sign that the two things really have something in common, not just chance. But real embeddings are not random directions. There is research showing that word vectors are not spread evenly in every direction (Ethayarajh, 2019). In that case, even unrelated things can have a cosine similarity well above 0. Thresholds like "above this means similar" should be measured for each model and each dataset.

Distance goes through something similar. In very high dimensions, for randomly scattered points, the distance to the nearest point and the distance to the farthest point become almost the same (Beyer et al., 1999). These effects together are called the curse of dimensionality. How it affects search is in section 9 of the Vector search page.

Try this. 1) At the start (n = 2), 7% of pairs fall between −0.1 and 0.1, 39% are above 0.3, and the spread is 0.71 (1/√2 = 0.71). 2) At n = 10: 25%, 18%, spread 0.31. 3) At n = 50: 51%, 1%, spread 0.14. 4) At n = 200: 85%, 0%, spread 0.07. The bars become one narrow hill around 0.

10. Key properties

PropertyIn plain wordsIn the experiments
Order matters(1, 2) and (2, 1) are different vectors. Each position has its own meaningExperiment 1: swap the values of bars v1 and v2 and the arrow points somewhere else
The dimensions must matchCalculations pair up positions, so a position without a partner leaves the answer undefinedExperiment 2: a and b always have the same n
The rules do not depend on the dimensionAdding, scaling, the dot product, length and distance are all done "once per position"Experiment 2: n from 2 to 8
Length and distance are Pythagoras, repeatedEach extra number adds one more square inside the root. Distance is the length of the difference vectorExperiment 3
Random directions are almost perpendicular in high dimensionsThe cosine similarity of two random directions piles up near 0, with a width of about 1/√nExperiment 5

Where it is used

Frequently asked questions

Is a vector always an arrow?

No. A vector is an ordered list of numbers. When it has 2 or 3 numbers you can draw it as an arrow, but with any number of numbers you add, scale and measure length by the same rules.

How do I picture 4 or more dimensions?

You do not have to. You calculate with the list of numbers, and to look at it you use bar charts, parallel coordinates, or a 2D picture made by squeezing the data (PCA). Be careful, though: what feels true in 2D and 3D can be wrong in high dimensions. For example, in high dimensions almost any two directions are close to perpendicular.

Can I add or take the dot product of vectors with different dimensions?

No. The calculations pair up positions, and a position that exists on only one side has no partner. The functions on this page return null instead of an answer in that case.

What is the difference between a matrix and a vector?

A vector is one line of numbers. A matrix is a table of numbers with m rows and n columns. You can see an m×n matrix as m vectors of n numbers stacked on top of each other, or as n vectors of m numbers placed side by side. A vector is a matrix with one column or one row (n×1 or 1×n).

Why do AI vectors have so many dimensions?

To hold many properties of a word or a sentence at once, you need many numbers. The people who build the model choose how many. Unlike the movie table on this page, the single numbers in an embedding do not have names that a person gave them, like "action".

Words

Words used hereWhat books call it
An ordered list of numbersVector. Also called an ordered tuple
The number in one positionComponent, entry. Written vi
How many numbersDimension
All vectors of n numbersℝn, real coordinate space, vector space
A single numberScalar
Multiplying every number by the same numberScalar multiplication
Multiply position by position and add upDot product, inner product
LengthNorm, Euclidean norm. Written ‖v‖
Straight-line distanceEuclidean distance
The "add them all up" signSigma, summation (Σ)
A vector standing up, a vector lying downColumn vector, row vector
Switching between standing up and lying downTranspose. Written aT
A vector of length 1Unit vector
n vertical axes with one bent lineParallel coordinates
Ways high dimensions break your intuitionCurse of dimensionality, distance concentration

Sources

← Cross productVector spaces and subspaces →