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

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")
主站蜘蛛池模板: 淮滨县| 黑水县| 安化县| 资溪县| 灵璧县| 彩票| 武乡县| 阜宁县| 万全县| 阿坝| 蓬安县| 长宁县| 金华市| 孟村| 北海市| 宁津县| 勃利县| 咸阳市| 乌兰浩特市| 凤城市| 阜平县| 红安县| 麦盖提县| 旌德县| 绍兴县| 永丰县| 仁怀市| 安图县| 峡江县| 吉安县| 定日县| 晋州市| 繁昌县| 商河县| 惠东县| 乌拉特中旗| 中卫市| 丰原市| 大荔县| 龙门县| 醴陵市|