Matrix Multiplication Using “For Loops”

[This article was first published on Blogs on Adejumo R.S, 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.

“That’s all data is. A gift from yesterday that you receive today to make tomorrow better”
— Jon Acuff

Linear Algebra is a branch of mathematics that deals with vectors and matrices, though some find it difficult, I still find it easier than good old calculus. Vectors and matrices are very important data structures in R, which is why the knowledge of linear algebra is very important. This post is going to use our understanding of “for loops” to explain matrix multiplication in R.
Firstly we are going to define two matrices, a which is a (4×3) matrix and b which is a (3×4) matrix, multiplying the two matrices will give us c which is a (4×4) matrix.

a <- matrix(c(9, 4 , 12, 5, 0, 7, 2, 6, 8, 9, 2, 9),
            nrow = 4, byrow = TRUE)

a
##      [,1] [,2] [,3]
## [1,]    9    4   12
## [2,]    5    0    7
## [3,]    2    6    8
## [4,]    9    2    9
b <- matrix(c(5, 4, 2, 5, 2, 7, 2, 1, 8, 3, 2, 6),
            nrow = 3, byrow = TRUE)

b
##      [,1] [,2] [,3] [,4]
## [1,]    5    4    2    5
## [2,]    2    7    2    1
## [3,]    8    3    2    6

The following code below is a function to perform the matrix multiplication between a and to b produce a matrix c.

mat_mup <- function(a, b){
  if(ncol(a) != nrow(b)){
    return("can't multiply")
  }
  else{
    c = matrix(rep(0, nrow(a)*ncol(b)), 
               nrow = nrow(a))
    for(i in 1:nrow(a)){
      for(j in 1:ncol(b)){
        for(k in 1:nrow(b)){
          c[i,j] <- c[i,j] + a[i,k]*b[k, j]
        }
      }
    }
  }
  return(c)
}

In the code above we create a function called mat_mup, the function returns “can’t multiply” if the number of columns in the matrix a does not match the number of rows in matrix b. The second condition performs the matrix operation making use of three for loops, the first for loop takes i values for the number of rows in the matrix a, the second for loop takes j for the number of columns in matrix b and the third for loop takes k for the number of rows in matrix b. The function returns a matrix c, the result of multiplying a and b.

mat_mup(a,b)
##      [,1] [,2] [,3] [,4]
## [1,]  149  100   50  121
## [2,]   81   41   24   67
## [3,]   86   74   32   64
## [4,]  121   77   40  101

Imagine having to always do the above just to multiply two matrices. R has a built-in operator that handles matrix multiplication. Ever taught why matrix a*b returns an error in R, well I leave that to you to figure out. The %*% operator is used to multiply matrices.

a %*% b
##      [,1] [,2] [,3] [,4]
## [1,]  149  100   50  121
## [2,]   81   41   24   67
## [3,]   86   74   32   64
## [4,]  121   77   40  101

To leave a comment for the author, please follow the link and comment on their blog: Blogs on Adejumo R.S.

R-bloggers.com 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.

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)