How to Multiply Matrix by Vector in R


A matrix is an object that has a specific number of rows and columns.

A vector, on the other hand, only has a specific number of values in one dimension.

Often you may want to multiply a matrix by a vector in R.

Fortunately this is easy to do because the R programming language was designed with matrix multiplication in mind.

Here are two common ways to multiply a matrix by a vector in R:

Method 1: Use %*%

#multiply matrix by vector
my_matrix %*% my_vector

This particular example uses the %*% operators to multiply a matrix named my_matrix by a vector named my_vector.

Method 2: Use rowSums() and apply()

#multiply matrix by vector
rowSums(t(apply(my_matrix, 1, function(x) my_vector*x)),na.rm=T)

This particular example uses the rowSums() and apply() functions to multiply a matrix named my_matrix by a vector named my_vector.

Note that this method is able to ignore NA values while the previous method is not.

Note: All functions listed in both of these methods come built-in with base R which means you do not need to install or load any external packages to use the function.

The following examples show how to use each of these methods in practice to multiply a matrix by a vector.

Related: How to Calculate Inverse Matrix in R

Example 1: Use %*% to Multiply a Matrix by a Vector in R

Suppose we create a matrix named my_matrix and a vector named my_vector:

#create matrix with six rows and three columns
my_matrix <- matrix(1:18, nrow=6)

#view matrix
my_matrix

     [,1] [,2] [,3]
[1,]    1    7   13
[2,]    2    8   14
[3,]    3    9   15
[4,]    4   10   16
[5,]    5   11   17
[6,]    6   12   18

#create vector
my_vector <- c(1, 2, 3)

#view vector
my_vector

[1] 1 2 3

Since the vector contains the same number of elements as the number of columns in the matrix, it is possible to multiply the vector by the matrix.

We can use the following syntax to do so:

#multiply matrix by vector
my_matrix %*% my_vector

     [,1]
[1,]   54
[2,]   60
[3,]   66
[4,]   72
[5,]   78
[6,]   84

This returns a matrix with one column and six rows.

Here is how the values were calculated in the resulting matrix:

  • First value: 1*1 + 2*7 + 3*13 = 54
  • Second value: 1*2 + 2*8 + 3*14 = 60
  • Third value: 1*3 + 2*9 + 3*15 = 66

And so on.

Example 2: Use rowSums() and apply() to Multiply a Matrix by a Vector in R

Suppose we create a matrix named my_matrix and a vector named my_vector:

#create matrix with six rows and three columns
my_matrix <- matrix(1:18, nrow=6)

#view matrix
my_matrix

     [,1] [,2] [,3]
[1,]    1    7   13
[2,]    2    8   14
[3,]    3    9   15
[4,]    4   10   16
[5,]    5   11   17
[6,]    6   12   18

#create vector
my_vector <- c(1, 2, 3)

#view vector
my_vector

[1] 1 2 3

Once again, the vector contains the same number of elements as the number of columns in the matrix, so it is possible to multiply the vector by the matrix.

We can use the following syntax to do so:

#multiply matrix by vector
rowSums(t(apply(my_matrix, 1, function(x) my_vector*x)),na.rm=T)

[1] 54 60 66 72 78 84

Notice that this returns the same six values as the previous method.

Note that if there were NA values in either the matrix or the vector that this method would be able to simply ignore the NA values when performing the multiplication.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Sort a Matrix in R
How to Remove NA from Matrix in R
How to Convert Data Frame to Matrix in R
How to Convert a Table to a Matrix in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *