MATRIX Using R

#library(Matrix)
my_mat <- matrix(c(3, 5, 6, 7, 3, 6, 2, 3, 6), byrow = TRUE, nrow = 3)
print(my_mat)
##      [,1] [,2] [,3]
## [1,]    3    5    6
## [2,]    7    3    6
## [3,]    2    3    6
my_mat2 <- matrix(1:9, byrow = TRUE, nrow = 3)
cat("\n")
print(my_mat2)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
# Finding Eigenvalues and Eigenvectors
L <- eigen(my_mat)$values
M <- eigen(my_mat)$vectors

cat("\n\nEigenvalues:\n")
## 
## 
## Eigenvalues:
print(L)
## [1] 13.172521 -2.799547  1.627026
cat("\nEigenvectors:\n")
## 
## Eigenvectors:
print(M)
##            [,1]       [,2]       [,3]
## [1,] -0.5926740 -0.5475662 -0.4180337
## [2,] -0.6707986  0.8221241 -0.6485442
## [3,] -0.4458326 -0.1558307  0.6361118
# Diagonalization
D <- solve(M) %*% my_mat %*% M
print(D)
##               [,1]          [,2]         [,3]
## [1,]  1.317252e+01 -2.220446e-15 1.776357e-15
## [2,] -6.133982e-15 -2.799547e+00 5.551115e-16
## [3,] -2.775558e-15 -8.881784e-16 1.627026e+00
# Sum of Rows
print("Sum of rows: ")
## [1] "Sum of rows: "
rowSums(my_mat)
## [1] 14 16 11
# Sum of Columns
print("Sum of columns: ")
## [1] "Sum of columns: "
colSums(my_mat)
## [1] 12 11 18
# Merge Matrix by column
long_mat <- cbind(my_mat, my_mat2)
cat("\n")
print(long_mat)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    3    5    6    1    2    3
## [2,]    7    3    6    4    5    6
## [3,]    2    3    6    7    8    9
# Merge Matrix by row
tall_mat <- rbind(my_mat, my_mat2)
cat("\n")
print(tall_mat)
##      [,1] [,2] [,3]
## [1,]    3    5    6
## [2,]    7    3    6
## [3,]    2    3    6
## [4,]    1    2    3
## [5,]    4    5    6
## [6,]    7    8    9
# Matrix Subsetting
tall_sub <- tall_mat[1:3, 1:3]
cat("\n")
print(tall_sub)
##      [,1] [,2] [,3]
## [1,]    3    5    6
## [2,]    7    3    6
## [3,]    2    3    6
## Select Row
cat("\n")
tall_sub[3,]
## [1] 2 3 6
## Select Column
cat("\n")
tall_sub[,3]
## [1] 6 6 6
# Transpose Matrix
t(my_mat)
##      [,1] [,2] [,3]
## [1,]    3    7    2
## [2,]    5    3    3
## [3,]    6    6    6