There's more…

Once you have created the matrix, then you can do matrix operations that are mathematically applicable, such as, matrix addition, subtraction, multiplication, inverse calculation, and many more. Matrix addition and subtraction are very much like the addition and subtraction of two numbers, but both matrices must have the same number of rows and columns. In other words, you cannot add or subtract two matrices if their number of rows and/or columns differ. From this recipe, matA and matB are not mathematically conformable for addition or subtraction, but matB and matE are conformable to do that operation:

    matADD <- matB + matE

To multiply one matrix by another matrix, the number of columns of the first matrix must be the same as of the number of rows of the second matrix. For example, the number of columns of matA is 2 and the number of rows in matC is 2, so you can multiply these two matrices as follows:

    matMult <- matA %*% matC

Notice that the matrix multiplication symbol is different from the multiplication symbol of two single numbers. For matrix multiplication, you must use %*%.

If you use the regular multiplication symbol and both matrices have the same number of rows and columns, then it will perform element-wise multiplication. In other words, it will multiply each corresponding element and create a new matrix as follows:

    matMult2 <- matB * matE #element-wise multiplication
matMult2 <- matB %*% matE #Matrix multiplication

Finally, you can also give the name of the rows and columns using the rownames() and colnames() functions as follows:

    rownames(matA) <- c("row1", "row2")
colnames(matA) <- c("col1", "col2")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset