官术网_书友最值得收藏!

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")
主站蜘蛛池模板: 寻乌县| 井冈山市| 天长市| 大埔县| 剑阁县| 会同县| 青冈县| 固原市| 璧山县| 翁牛特旗| 周口市| 英吉沙县| 临沧市| 呼伦贝尔市| 阿拉尔市| 红安县| 喀喇| 安陆市| 棋牌| 四会市| 左贡县| 仪征市| 离岛区| 怀远县| 鄂托克前旗| 金堂县| 大邑县| 通江县| 祁东县| 鸡西市| 山西省| 台北县| 普兰县| 小金县| 汤阴县| 镇江市| 宜阳县| 襄城县| 厦门市| 家居| 青川县|