Skip to content
Glacius
Linear algebraConcept reference

Projection onto a line

The orthogonal projection of a vector onto the span of a nonzero vector is the multiple whose difference from the original vector is perpendicular to that span.

On this page 8 sections
  1. Overview
  2. Projection finds the part of a vector that points along a chosen line
  3. Write p=cu
  4. The coefficient can be negative or zero
  5. This is the geometry behind fitting a one-feature linear model with no intercept
  6. Key takeaway
  7. Further questions
  8. Concept connections
With b=(2,3) and u=(1,0), the line span(u) is horizontal through O. Its orthogonal projection is p=(2,0). The dashed gap from p to b is perpendicular to the line. Equal scale is 30 pixels per coordinate unit; b is longer than p.With b=(2,3) and u=(1,0), the line span(u) is horizontal through O. Its orthogonal projection is p=(2,0). The dashed gap from p to b is perpendicular to the line. Equal scale is 30 pixels per coordinate unit; b is longer than p.
Figure 1With b=(2,3) and u=(1,0), the line span(u) is horizontal through O. Its orthogonal projection is p=(2,0). The dashed gap from p to b is perpendicular to the line. Equal scale is 30 pixels per coordinate unit; b is longer than p.
Link to this figure ↗Download SVGDownload PNG

01Projection finds the part of a vector that points along a chosen line#

Projection finds the part of a vector that points along a chosen line. Imagine fitting a target vector using only one allowed direction. You may slide along that line, but cannot move sideways; projection chooses its closest point to the target.

The line span(u) passes through the origin. Its closest point to the tip of b defines the projected vector p. The gap from p to b is perpendicular to the line.

Use b=(3,1)b=(3,1) and u=(1,1)u=(1,1). Their dot is 4; u·u=2. The coefficient is 4/2=2, so p=2u=(2,2).

For b=(3,1),u=(1,1), the projection is p=(2,2). The line through O in direction u contains p; the dashed vector from p to b is (1,-1), perpendicular to u. Equal scale is 32 pixels per coordinate unit.For b=(3,1),u=(1,1), the projection is p=(2,2). The line through O in direction u contains p; the dashed vector from p to b is (1,-1), perpendicular to u. Equal scale is 32 pixels per coordinate unit.
Figure 2For b=(3,1),u=(1,1), the projection is p=(2,2). The line through O in direction u contains p; the dashed vector from p to b is (1,-1), perpendicular to u. Equal scale is 32 pixels per coordinate unit.
Link to this figure ↗Download SVGDownload PNG

For target b=(2,4)b=(2,4) and direction a=(1,1)a=(1,1), compute ab=6a\cdot b=6 and aa=2a\cdot a=2. Their ratio 3 is a coefficient. Multiply the entire direction by it to get projected vector (3,3)(3,3). The leftover b(3,3)=(1,1)b-(3,3)=(-1,1) has dot product zero with aa, checking that no along-line correction remains.

Check your reasoning

b=(2,6)b=(2,6), u=(1,1)u=(1,1). Project b onto span(u).

  1. A(4,4)
  2. B(8,8)
  3. C(-2,2)
Show answer and explanation
(4,4)

The coefficient is 8/2=4.

02Write p=cu#

Write p=cu. A perpendicular gap requires (bcu)u=0(b-cu)\cdot u=0. Solving for c gives the rule, with u0u\ne0:

p=buuuup=\frac{b\cdot u}{u\cdot u}\,u
Check your reasoning

b=(3,1)b=(3,1), u=(2,0)u=(2,0). Find c in the projection p=cu.

Show answer and explanation
1.5

c=6/4=1.5.

03The coefficient can be negative or zero#

The coefficient can be negative or zero. If b·u=0, then p=0. A projection can shorten a vector; it keeps b unchanged only when b already lies on the line. The zero vector cannot supply a line direction.

Check your reasoning

A rail allows multiples of u=(0,2)u=(0,2). Desired b=(1,3)b=(1,-3). Closest allowed move?

  1. A(1,-3)
  2. B(0,-3)
  3. C(0,-12)
Show answer and explanation
(0,-3)

The coefficient is −6/4=−1.5.

04This is the geometry behind fitting a one-feature linear model with no intercept#

This is the geometry behind fitting a one-feature linear model with no intercept. The coefficient says how much of the allowed pattern to use; the projected vector contains the fitted outputs. Those two answers have different shapes and should not be interchanged.

Key takeaway

Compute the coefficient, then scale the nonzero direction.

  • Compute a complete projection vector onto a nonzero direction’s span

Further questions

Why divide by u·u?
The spanning vector need not be a unit vector. Dividing by its squared length makes the perpendicular-gap condition hold. For u=0 the denominator vanishes and u does not span a line.

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Projection onto a line.” Math behind ML. /learn/la-line-projection