w3resource

R Programming: Create Two 2x3 Matrices and Perform Operations


Write a R program to create two 2x3 matrix and add, subtract, multiply and divide the matrixes.

Sample Solution:

R Programming Code:

# Create the first 2x3 matrix with elements 1 to 6
m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)

# Print a message indicating the following output is Matrix-1
print("Matrix-1:")

# Print the contents of the first matrix
print(m1)

# Create the second 2x3 matrix with elements 0, 1, 2, 3, 0, 2
m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)

# Print a message indicating the following output is Matrix-2
print("Matrix-2:")

# Print the contents of the second matrix
print(m2)

# Perform element-wise addition of the two matrices
result = m1 + m2

# Print a message indicating the following output is the result of addition
print("Result of addition")

# Print the result of the matrix addition
print(result)

# Perform element-wise subtraction of the two matrices
result = m1 - m2

# Print a message indicating the following output is the result of subtraction
print("Result of subtraction")

# Print the result of the matrix subtraction
print(result)

# Perform element-wise multiplication of the two matrices
result = m1 * m2

# Print a message indicating the following output is the result of multiplication
print("Result of multiplication")

# Print the result of the matrix multiplication
print(result)

# Perform element-wise division of the two matrices
result = m1 / m2

# Print a message indicating the following output is the result of division
print("Result of division:")

# Print the result of the matrix division
print(result)

Output:

[1] "Matrix-1:"
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[1] "Matrix-2:"
     [,1] [,2] [,3]
[1,]    0    2    0
[2,]    1    3    2
[1] "Result of addition"
     [,1] [,2] [,3]
[1,]    1    5    5
[2,]    3    7    8
[1] "Result of subtraction"
     [,1] [,2] [,3]
[1,]    1    1    5
[2,]    1    1    4
[1] "Result of multiplication"
     [,1] [,2] [,3]
[1,]    0    6    0
[2,]    2   12   12
[1] "Result of division:"
     [,1]     [,2] [,3]
[1,]  Inf 1.500000  Inf
[2,]    2 1.333333    3           

Explanation:

  • Create Matrix m1:
    • A 2x3 matrix m1 is created with values 1 through 6.
  • Print Matrix m1:
    • The matrix m1 is printed along with the label "Matrix-1:".
  • Create Matrix m2:
    • A 2x3 matrix m2 is created with values 0, 1, 2, 3, 0, and 2.
  • Print Matrix m2:
    • The matrix m2 is printed along with the label "Matrix-2:".
  • Add Matrices m1 and m2:
    • The element-wise addition of matrices m1 and m2 is computed and the result is printed.
  • Subtract Matrix m2 from m1:
    • The element-wise subtraction of matrix m2 from matrix m1 is computed and the result is printed.
  • Multiply Matrices m1 and m2:
    • The element-wise multiplication of matrices m1 and m2 is computed and the result is printed.
  • Divide Matrix m1 by m2:
    • The element-wise division of matrix m1 by matrix m2 is computed and the result is printed.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to access the element at 3rd column and 2nd row, only the 3rd row and only the 4th column of a given matrix.
Next: Write a R program to create a matrix from a list of given vectors.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.