R Lang

R Program to Multiply Two Matrix Using Multi-dimensional Arrays1 min read

In R, a matrix is a two-dimensional array-like data structure, where elements are stored in rows and columns. Matrices are a powerful data structure for numerical computations and are commonly used in many areas, including linear algebra, statistics, and data analysis. In R, matrices can be created using the matrix function, and you can access elements in a matrix using row and column indices. Matrices can be manipulated using various functions and operators, including addition, subtraction, multiplication, and transposition.

Here’s a simple example of creating a matrix in R:

The output will be:

In this example, we created a matrix with 2 rows and 2 columns, and assigned it to the variable mat. The elements of the matrix are stored in the vector c(1,2,3,4), and we set byrow = TRUE to indicate that the elements should be filled in by rows.

You may also like: What is Matrix in R? Explained with Examples

Solution: In R, you can multiply two matrices using the %*% operator. Here’s an example

The output will be:

In this example, we first create two matrices mat1 and mat2, and then multiply them using the %*% operator to get the result matrix. The product of two matrices is calculated using the standard matrix multiplication formula, where each element in the resulting matrix is the dot product of the corresponding row in the first matrix and the corresponding column in the second matrix.

Leave a Comment