Elementwise Matrix Multiplication in R Last Updated : 26 Mar, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. In this article, we are going to perform element-wise matrix multiplication in R programming. Approach Create a matrixMultiply two matricesVerify the result. Element-wise multiplication using "*" operator: Syntax: matrix1*matrix*2.....matrix n Example 1: This code shows the element-wise multiplication of two matrices data1 and data2, Data comprises 5 rows and 2 columns: R # Creating matrices 10 elements each using # range operator ":" data1 <- matrix(1:10, nrow = 5) print(data1) data2 <- matrix(11:20, nrow = 5) print(data2) # perform element wise multiplication print(data1*data2) Output: Example 2: This code for multiplication of multiple matrices data1,data2,data3. All data comprises 5 rows created using the range operator. R # Creating matrices 10 elements each # using range operator ":" data1 <- matrix(1:10, nrow = 5) print(data1) data2 <- matrix(11:20, nrow = 5) print(data2) data3 <- matrix(21:30, nrow = 5) # perform element wise multiplication print(data1*data2*data3) Output: Example 3: This code shows the matrix is created using vectors. And matrix multiplication is done. R # vector a a = c(3, 4, 5, 6, 7, 8) # vector b b=c(1, 3, 0, 7, 8, 5) # Creating matrices using vector data1 <- matrix(a, nrow = 3) print(data1) data2 <- matrix(b, nrow = 3) print(data2) print(data1*data2) Output: Example 4: An example that shows multiplication column arrangement and matrices data1 and data2 and multiplied. Column wise we are going to perform matrix multiplication data1 and data2 comprises 3 columns and elements are created using vector. R # vector a a = c(3, 4, 5, 6, 7, 8) # vector b b = c(1, 3, 0, 7, 8, 5) # Creating matrices using vector data1 <- matrix(a, ncol = 3) print(data1) data2 <- matrix(b, ncol = 3) print(data2) print(data1*data2) Output: Comment More infoAdvertise with us Next Article Divide Each Row of Matrix by Vector Elements in R S sravankumar_171fa07058 Follow Improve Article Tags : R Language R Programs R-Matrix R Matrix-Programs Similar Reads Multiply Matrix by Vector in R A matrix is a 2-dimensional structure whereas a vector is a one-dimensional structure. In this article, we are going to multiply the given matrix by the given vector using R Programming Language. Multiplication between the two occurs when vector elements are multiplied with matrix elements column-wi 2 min read Get element at the specific position from matrix in R At any point in time, a matrix may be required to be traversed for an element at a specific position. In this article, we are going to access the elements from a matrix in R Programming Language using integer vector, and logical vector as the index. Accessing Elements in a MatrixIn R Programming Lan 2 min read Convert Matrix to Vector in R In this article, we are going to convert the given matrix into the vector in R programming language. Conversion of the matrix to vector by rowMethod 1: Using c() function Simply passing the name of the matrix will do the job. Syntax: c(matrix_name) Where matrix_name is the name of the input matrix E 3 min read Find product of vector elements in R In this article, we will see how to find the product of vector elements in R programming language. Method 1: Using iteration Approach Create dataframeIterate through the vectorMultiply elements as we goDisplay product The following code snippet indicates the application of for loop over decimal poin 2 min read Divide Each Row of Matrix by Vector Elements in R In this article, we will discuss how to divide each row of the matrix by vector elements in R Programming Language. Method 1: Using standard division Initially, the transpose of the matrix is computed, to interchange the rows and columns. Initially, if the dimensions of the matrix were n * m , trans 6 min read How to Create the Identity Matrix in R? In this article, we will discuss how to create an Identity Matrix in R Programming Language. Identity matrices are the matrices that contain all the zeros, except diagonal elements which are equivalent to 1. The identity matrices are always square in nature. Base R provides a large number of methods 3 min read Like