Page 232 - Data Science class 11
P. 232

This will cause the following to be displayed:
































        6.9.4 matrix arithmetical computations

        Various mathematical operations such as addition, subtraction, multiplication, and division can also be performed on
        matrices using the R operators. The result of the operation is also a matrix.
        The dimensions (number of rows and columns) should be the same for the matrices involved in the operation.

        a.  Matrix Addition & Subtraction

        Example
        Enter the following code snippet:


             # Create two 2x3 matrices.
             matrix1 <- matrix(c(2, 9, -1, 4, 2, 5), nrow = 2)
             print(matrix1)

             matrix2 <- matrix(c(4, 2, 0, 9, 3, 8), nrow = 2)
             print(matrix2)
             # Add the two matrices.
             result <- matrix1 + matrix2

             cat("Result of addition","\n")
             print(result)
             # Subtract the matrices

             result <- matrix1 - matrix2
             cat("Result of subtraction","\n")
             print(result)







          230   Touchpad Data Science-XI
   227   228   229   230   231   232   233   234   235   236   237