Progress Log 110 (R): Matrix Arithmetic

I’m taking an edX course entitled “Introduction to R for Data Science” and all of the concepts described below come from that course.

Matrix arithmetic

  • colSums() – takes the sum of each column and stores the result in a vector.
  • rowSums() – takes the sum of each row and stores the result in a vector.
  • Standard arithmetic possible.

lotr_matrix

Here is a matrix of box office revenue for the Lord of the Rings for both US and non-US regions. The information is saved in a matrix, lotr_matrix, which has been constructed as follows:

These are astronomical numbers, we’re talking about millions of US dollars here.

Matrix-Scalar

What if we wanted to convert this to Euros? At the time of writing, 1 euro converts to 1.12 USD, so to convert the figures to euros, we’ll have to divide the figures by 1.12. We can simply use the division operator as if we were performing the division on a single number:

This works just the same for multiplication, summation, and subtraction. Say, for example, that the theaters world-wide claim 50 million dollars of the box office revenue. How much is left for the Lord of the Rings itself then? We simply subtract 50 from the LOTR matrix:

Matrix-Matrix

Operating on matrices with single numbers looks pretty straightforward and this actually holds when you’re performing calculations with two matrices.

Suppose that instead of demanding the same sum of money for every release, theaters worldwide ask for 50 million for the first release, 80 million for the second, and 100 for the third. We could build a matrix as follows:

  • theater_cut has the same dimensions as lotr_matrix.
  • the subtraction was also performed element wise: the figures for the Two Towers lowered by 80, while the figures for the Return of the King were lowered by 100.

Recycling

What would happen if we use a vector containing 50, 80, and 100 to subtract from The Lord of the Rings matrix?

The result is exactly the same. This is because, once again, R performed recycling. R realizes that the dimensions of the matrix and the vector don’t match. Therefore, the vector is extended to a matrix of the same size, and is filled up with the vector elements column by column.

The matrix that is thus actually subtracted from the lotr matrix is the following:

Blinding trusting that R will perform recycling just the way you want it can be quite a dangerous practice. You should be fully aware of how this recycling is actually happening.

Matrix Multiplication

In R, multiplication is performed element wise.

Suppose you want to convert the US dollar figures to euros with the exchange rate at the time of the release. You can create a new matrix, this time with the amount of euros you should pay for 1 dollar.

Now, we can simply multiply the lord of the rings matrix with the rates matrix.

Matrices and Vectors

  • Very similar; they are simply data structures that store elements of the same type.
  • Vector = 1D, Matrix = 2D
  • Coercion if necessary
  • Recycling if necessary
  • All calculations are performed element-wise.

Practice

Leave a Comment