The provided matices
A = 2,0,1,3
B = 5,2,4,-1
Add and subtract the matrices.
A <- matrix(c(2,0,1,3), ncol=2)
B <- matrix(c(5,2,4,-1), ncol=2)
A+B
## [,1] [,2]
## [1,] 7 5
## [2,] 2 2
A-B
## [,1] [,2]
## [1,] -3 -3
## [2,] -2 4
Use diag() function to build a matrix with 4,1,2,3 the diagonal.
vect <- c(4,1,2,3)
diag(vect)
## [,1] [,2] [,3] [,4]
## [1,] 4 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 2 0
## [4,] 0 0 0 3
Generate sample matrix
dm <- diag(3,5,5)
a <- c(1,0,0,0,0)
sdm1 <- sweep(dm,1,a,"+")
b <- c(2,0,0,0,0)
sdm2 <- sweep(sdm1,2,b,"+")
sdm2[1,1] <- sdm2[sdm2[2],sdm2[2]]
sdm2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3 1 1 1 1
## [2,] 2 3 0 0 0
## [3,] 2 0 3 0 0
## [4,] 2 0 0 3 0
## [5,] 2 0 0 0 3
GitHub
Related file(s) can be found at Git Me
No comments:
Post a Comment