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.
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.
- 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.
- norm
- The positive square root of the sum of the squares of the components. It is also called the Euclidean norm.
- Euclidean distance
- The norm of the difference of two vectors.
- 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.
- 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.
- 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.
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 seeing | How you see (3, 4) | When it helps |
|---|---|---|
| List of numbers | 3 in the first position, 4 in the second | Always works. This is how a computer stores it (an array) |
| Point | The spot (3, 4) on a plane | When one record is one point in space. Close points = similar records |
| Arrow | An 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
- A vector's name is printed in bold in books, like v. By hand, people put a small arrow over the v. This page just writes v.
- The number in position i is written vi ("v sub i"). So
v = (v1, v2, …, vn). Math counts from 1 and arrays in code count from 0, so v1 isv[0]in code. - ℝn ("R n") is the set of all lists of n real numbers. ℝ means ordinary numbers that can have decimals (real numbers).
v ∈ ℝ4means "v is a vector of 4 numbers". In TypeScript terms, it is anumber[]whose length is always 4.
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.
| What | The list of numbers | Dimension |
|---|---|---|
| The color of one pixel | How 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 ball | Position (x, y, z) followed by velocity (x, y, z) | 6 |
| One 28 × 28 grayscale image | One brightness per pixel, written out in one long line. The handwritten digit collection MNIST uses this size | 784 |
| A word or sentence embedding | The numbers a model outputs | Hundreds 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.
- Bar chart. One bar per position. This works even with hundreds of dimensions. Use it to look closely at one vector.
- Parallel coordinates. Stand n vertical axes side by side, mark each position's number on its axis, and join the marks with a bent line. One vector = one bent line. If you draw many vectors on top of each other, similar vectors make similar lines, and groups become visible (Inselberg, 1985).
- Squeeze to 2D. Keep only the two directions where the data spreads the most, and plot points. You get a map where similar things sit together, but you lose the information in the directions you dropped (PCA).
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.
| Calculation | Formula (for each position i) | What you get |
|---|---|---|
| Add | a + 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 product | a · b = Σ ai × bi | One 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.
| Calculation | Position by position | Result |
|---|---|---|
| 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 · b | 1×2 + 2×1 + 2×0 + 4×2 = 2 + 2 + 0 + 8 | 12 |
| ‖a‖ | √(1² + 2² + 2² + 4²) = √(1 + 4 + 4 + 16) = √25 | 5 |
| ‖b‖ | √(2² + 1² + 0² + 2²) = √(4 + 1 + 0 + 4) = √9 | 3 |
| 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) = √10 | about 3.16 |
| Cosine similarity | 12 ÷ (5 × 3) = 12 ÷ 15 | 0.8 |
Written as equations, the same work looks like this.
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.
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.
(3, 4, 2) in space is the diagonal of a box. First find the diagonal of the floor, from 0 to (3, 4), which is 5. Then make one more right triangle from that 5 and the height 2. Since 5² is 3² + 4², only one new piece, z², joins the inside of the square root.
Each extra number means doing the same thing once more. Make a right triangle from the length so far and the new number, and one more square joins the inside of the root. You cannot draw 4-dimensional space, but you can keep this calculation going as many times as you like. A negative number squared is positive, so (−1)² = 1.
The distance between two points is the length of the arrow from one to the other. That arrow is the difference a − b, so subtract position by position and put the result into the length formula above. The two finished formulas are ‖v‖ = √(Σ vi²) and ‖a − b‖ = √(Σ (ai − bi)²).
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).
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
| Property | In plain words | In the experiments |
|---|---|---|
| Order matters | (1, 2) and (2, 1) are different vectors. Each position has its own meaning | Experiment 1: swap the values of bars v1 and v2 and the arrow points somewhere else |
| The dimensions must match | Calculations pair up positions, so a position without a partner leaves the answer undefined | Experiment 2: a and b always have the same n |
| The rules do not depend on the dimension | Adding, 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, repeated | Each extra number adds one more square inside the root. Distance is the length of the difference vector | Experiment 3 |
| Random directions are almost perpendicular in high dimensions | The cosine similarity of two random directions piles up near 0, with a width of about 1/√n | Experiment 5 |
Where it is used
- AI search and recommendations. Text, images, products and people's tastes are turned into vectors with the same dimension, and nearby vectors are found (Embeddings, Vector search). The movie table in section 7 is a tiny version of this.
- Working with images. A 28 × 28 grayscale image is a 784-dimensional vector, one brightness per pixel. A neural network takes this vector and guesses the digit (Neural networks).
- One layer of a neural network. It multiplies the input vector by a matrix and gets a vector with a different dimension, for example from 784 to 128 (Matrix multiplication).
- Physics and games. If you treat the position and velocity of an object as one 6-dimensional vector, you can get its state one step later with vector arithmetic.
- Analyzing tables. If each row of a table is a vector, you can use distance to find similar rows, split rows into groups, and squeeze them into 2D to draw a map (PCA).
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 here | What books call it |
|---|---|
| An ordered list of numbers | Vector. Also called an ordered tuple |
| The number in one position | Component, entry. Written vi |
| How many numbers | Dimension |
| All vectors of n numbers | ℝn, real coordinate space, vector space |
| A single number | Scalar |
| Multiplying every number by the same number | Scalar multiplication |
| Multiply position by position and add up | Dot product, inner product |
| Length | Norm, Euclidean norm. Written ‖v‖ |
| Straight-line distance | Euclidean distance |
| The "add them all up" sign | Sigma, summation (Σ) |
| A vector standing up, a vector lying down | Column vector, row vector |
| Switching between standing up and lying down | Transpose. Written aT |
| A vector of length 1 | Unit vector |
| n vertical axes with one bent line | Parallel coordinates |
| Ways high dimensions break your intuition | Curse of dimensionality, distance concentration |
Sources
- MIT OCW 18.06 Linear Algebra. Gilbert Strang, Introduction to Linear Algebra, chapter 1 (vectors, lengths and dot products).
- Alfred Inselberg (1985). The plane with parallel coordinates. The Visual Computer. Parallel coordinates.
- Avrim Blum, John Hopcroft, Ravindran Kannan (2020). Foundations of Data Science. Cambridge University Press. Chapter 2, High-Dimensional Space.
- Beyer, Goldstein, Ramakrishnan, Shaft (1999). When Is "Nearest Neighbor" Meaningful? ICDT 1999. Distances becoming alike in high dimensions.
- Kawin Ethayarajh (2019). How Contextual are Contextualized Word Representations? EMNLP 2019. The finding that word vectors are not spread evenly in every direction.
- LeCun, Bottou, Bengio, Haffner (1998). Gradient-based learning applied to document recognition. Proceedings of the IEEE. The 28 × 28 handwritten digit collection (MNIST).
- Mikolov, Chen, Corrado, Dean (2013). Efficient Estimation of Word Representations in Vector Space. A way to represent words as vectors (word2vec).