Matrix Multiplication in R
Last Updated :
12 Mar, 2024
Matrix multiplication is the most useful matrix operation. It is widely used in areas such as network theory, transformation of coordinates and many more uses nowadays. A matrix in R can be created using matrix() function and this function takes input vector, nrow, ncol, byrow, dimnames as arguments.
Creating a matrix
A matrix can be created using matrix() function.
R
# R program to create a matrix
m <- matrix(1:8, nrow=2)
print(m)
Output:
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
Multiplication of Matrices
The multiplication operator * is used for multiplying a matrix by scalar or element-wise multiplication of two matrices.
Multiplication with scalar
If you multiply a matrix with a scalar value, then every element of the matrix will be multiplied with that scalar.
R
# R program for matrix multiplication
# with a scalar
m <- matrix(1:8, nrow=2)
m <- 2*m
print(m)
Output:
[,1] [,2] [,3] [,4]
[1,] 2 6 10 14
[2,] 4 8 12 16
In the above code, the scalar is multiplied with every element of the original matrix. This is how the multiplication process takes place.
2*1=2 2*3=6 2*5=10 2*7=14
2*2=4 2*4=8 2*6=12 2*8=16
Multiplication between Matrices
When a matrix is multiplied with another matrix, the element-wise multiplication of two matrices take place. All the corresponding elements of both matrices will be multiplied under the condition that both matrices will be of the same dimension.
R
# R program for matrix multiplication
# Creating matrices
m <- matrix(1:8, nrow=2)
n <- matrix(8:15, nrow=2)
# Multiplying matrices
print(m*n)
Output:
[,1] [,2] [,3] [,4]
[1,] 8 30 60 98
[2,] 18 44 78 120
This is how the multiplication process takes place.
1*8=8 3*10=30 5*12=60 7*14=98
2*9=18 4*11=44 6*13=78 8*15=120
Multiplication with Vector
If a matrix is multiplied with a vector then vector will be promoted to either row or column matrix to make two arguments conformable.
R
# R program for matrix multiplication
# Creating matrix
m <- matrix(1:8, nrow=2)
# Creating a vector
vec <- 1:2
# Multiplying matrix with vector
print(vec*m)
Output:
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 4 8 12 16
This is how the multiplication process takes place:
1*1=1 1*3=3 1*5=5 1*7=7
2*2=4 2*4=8 2*6=12 2*8=16
Multiplication using %*% operator
The Operator%*% is used for matrix multiplication satisfying the condition that the number of columns in the first matrix is equal to the number of rows in second. If matrix A[M, N] and matrix B[N, Z] are multiplied then the resultant matrix will be of dimension M*Z.
R
# R program for matrix multiplication
# Creating matrices
m <- matrix(1:8, nrow=2)
n <- matrix(8:15, nrow=4)
# Multiplying matrices using operator
print(m %*% n)
Output:
[,1] [,2]
[1,] 162 226
[2,] 200 280
This is how multiplication takes place
1*8+3*9+5*10+7*11 = 162 1*12+3*13+5*14+7*15=226
2*8+4*9+6*10+8*11 = 200 2*12+4*13+6*14+8*15=280
Similar Reads
Matrix multiplication in R using for loop
The matrix multiplication is a basic operation in the linear algebra. In R Programming Language we can perform matrix multiplication using a for loop, which provides a basic understanding of the underlying process involved. In R language, we can perform matrix multiplication using a for loop, which
6 min read
Operations on Matrices in R
Matrices in R are a bunch of values, either real or complex numbers, arranged in a group of fixed number of rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets. A matrix with
8 min read
How to perform multiplication in R
Multiplication is a fundamental arithmetic operation that is essential in various fields, including data analysis, statistics, and machine learning. The R Programming Language provides robust support for performing multiplication, whether it's simple scalar multiplication, element-wise multiplicatio
3 min read
Matrix in R - Arithmetic Operations
Arithmetic operations include addition (+), subtraction (-), multiplication(*), division (/) and modulus(%). In this article we are going to see the matrix creation and arithmetic operations on the matrices in R programming language. ApproachCreate first matrix Syntax: matrix_name <- matrix(data
3 min read
R - DataFrame Manipulation
Data Frame is a two-dimensional structured entity consisting of rows and columns. It consists equal length vectors as rows. The data is stored in cells which are accessed by specifying the corresponding [row, col] set of values of the data frame. Manipulation of data frames involve modifying, extrac
9 min read
Algebraic Operations on a Matrix in R
In this article, we will discuss how we perform Algebraic Operations on a Matrix in R Programming Language. so we will start with the matrix.What is Matrix?A Matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and column
8 min read
Multidimensional Array in R
Arrays are the R data objects which can store data in more than two dimensions. For example: If we create an array of dimensions (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns. These types of arrays are called Multidimensional Arrays. Arrays can store only data types
3 min read
Array vs Matrix in R Programming
The data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. The two most important data structures in R ar
3 min read
Matrix Transpose in R
Transpose of a matrix is an operation in which we convert the rows of the matrix in column and column of the matrix in rows. The general equation for performing the transpose of a matrix is as follows. Aij = Aji  where i is not equal to j Example: Matrix M ---> [1, 8, 9 12, 6, 2 19, 42, 3] Transp
2 min read
Simulink in MATLAB
MATLAB which is also known as "Matrix Laboratory" allows its user to manipulate matrixes, plotting of functions and data, creation of algorithms, and user interfaces written in different programming languages by providing a computing environment. MATLAB is software designed and powered by mathworks.
4 min read