- 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")
- What's New in TensorFlow 2.0
- Monkey Game Development:Beginner's Guide
- 編寫高質(zhì)量代碼:改善Python程序的91個建議
- 老“碼”識途
- iOS應(yīng)用逆向工程(第2版)
- 從零開始學(xué)C語言
- Getting Started with React Native
- 現(xiàn)代C++編程實(shí)戰(zhàn):132個核心技巧示例(原書第2版)
- Instant jQuery Boilerplate for Plugins
- 創(chuàng)意UI Photoshop玩轉(zhuǎn)移動UI設(shè)計(jì)
- Greenplum構(gòu)建實(shí)時數(shù)據(jù)倉庫實(shí)踐
- Python物理建模初學(xué)者指南(第2版)
- 零基礎(chǔ)學(xué)SQL(升級版)
- Daniel Arbuckle's Mastering Python
- Learning Java by Building Android Games