- Modern R Programming Cookbook
- Jaynal Abedin
- 271字
- 2021-07-08 09:48:31
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")
- DB2 V9權(quán)威指南
- 程序員面試筆試寶典(第3版)
- Docker and Kubernetes for Java Developers
- C#程序設(shè)計(jì)實(shí)訓(xùn)指導(dǎo)書
- Mastering OpenCV Android Application Programming
- C#編程入門指南(上下冊(cè))
- 基于差分進(jìn)化的優(yōu)化方法及應(yīng)用
- JavaScript:Moving to ES2015
- 學(xué)習(xí)正則表達(dá)式
- Flutter跨平臺(tái)開發(fā)入門與實(shí)戰(zhàn)
- SQL經(jīng)典實(shí)例(第2版)
- Mastering Git
- 區(qū)塊鏈技術(shù)進(jìn)階與實(shí)戰(zhàn)(第2版)
- QGIS Python Programming Cookbook(Second Edition)
- C++語(yǔ)言程序設(shè)計(jì)