Deep (learning) like Jacques Cousteau – Part 5 – Vector addition

[This article was first published on Embracing the Random | R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(TL;DR: You can add vectors that have the same number of elements.)

LaTeX and MathJax warning for those viewing my feed: please view
directly on website!

Mos def-11-mika

You want to know how to rhyme, you better learn how to add
It’s mathematics

Mos Def in ‘Mathematics’

we learnt about scalar multiplication. Let’s get to adding vectors

Today’s topic: vector addition

We will follow the notation in Goodfellow, Ian, et
. If our vector
has elements that are real numbers, then we can say that
is a -dimensional vector.

We can also say that lies in some set of all vectors
that have the same dimensions as itself
. This might be a bit abstract at first,
but it’s not too bad at all.

Let’s define two vectors:

They have two elements. They are, therefore, two-dimensional vectors.

What are some other two dimensional vectors made up of real numbers? We
could have:

There are infinitely many vectors with two elements that we could come up
with! How can we describe this infinite set of vectors made up of real
numbers? We can say that our two-dimensional vectors made up of real
numbers lie in this set:

What in the world does this mean?

Cartesian products

To understand what the above product means, let’s use a simplified
example. Let’s define sets of integers like so:

What is the result of ? We can depict this operation in a

What we’re doing here is taking an arbitrary element from our first set,
. We are then pairing it up with a second arbitrary element from the
set, . The entries in the matrix form a set of ordered pairs
(i.e. the order of elements in the pair of numbers matter)
which can
be defined like so:

This is called the Cartesian product of two sets. Translating the
equation into plain English gives us this:

Now let’s replace the terminology of ordered pairs with the word
vectors. If we depict the vectors as row vectors, our matrix now
looks like this:

Now let’s replace sets and with the set of real numbers,
. The Cartesian product then becomes:

We now have a set of ordered pairs which are drawn from the Cartesian
product of the set of real numbers with itself!
If we again replace
the term ‘ordered pairs’ with ‘vectors’, we now have the set
of all two dimensional, real number vectors
. We can finally say this about the vectors we defined earlier:


Now it’s not difficult to see that is the set formed by
taking the -ary Cartesian product of the set of real numbers. We
can now quite easily generalise to vectors of dimensions.

Get to the point! When can we add vectors?

If we have vectors of equal dimensions, we simply add the elements
of the vectors, one element at a time!
The resulting vector is a vector of
the same dimensions as the vectors that were just added.

Let’s use our example vectors from before:

We know that . We now know that
. Let’s add them:


How can we add vectors in R?

Some operations on vectors are defined differently to those that are
defined in linear algebra.

When we have two numeric vectors with the same number of elements, we
can add them just like we saw above:

x <- c(1, 2, 3)
y <- c(2, 3, 4)

x +  y
## [1] 3 5 7

But what if we try to add two numeric vectors with different numbers
of elements?

x <- c(1, 2)
y <- c(1, 1, 1, 1)
x + y
## [1] 2 3 2 3

What the hell just happened? This isn’t what we have just learnt! How is it
possible to add these two vectors given the mathematical definition of
vector addition?

If we look at the help page for +, we can find our answer:


The binary operators return vectors containing the result of the
element by element operations. If involving a zero-length vector the
result has length zero. Otherwise, the elements of shorter vectors
are recycled as necessary (with a warning when they are recycled only

This is what R is doing:

  • For the first two elements of the longer vector, R is adding
    the shorter vector to it in the above mathematically defined
  • When it runs out of elements in the shorter vector, R goes
    back to the first element of the shorter vector and adds this
    to the next element of the longer vector.
  • R continues like this until it runs out of elements in the longer

(Man, that’s a lot of use of the word vector!)

In this way, R allows you to add vectors of unequal length. We also told
that some other operators also behave in this way:

The operators are + for addition, – for subtraction, * for
multiplication, / for division and ^ for exponentiation.

These aren’t our focus here so let’s move on.

What about adding scalars to vectors?

In our standard mathematical definition, we can’t add scalars to
vectors. But we can in R!

We simply add the scalar to our vector, one element at a time:

x <- 1
y <- c(1, 2, 3)

x + y
## [1] 2 3 4


We learnt how to add vectors. We also learnt a little bit more about
sets. We learnt that R can behave differently to our mathematical
definitions of vector addition. Let’s now move onto dot products!

To leave a comment for the author, please follow the link and comment on their blog: Embracing the Random | R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)