Solve following using Program R studio. Please show code and results. Thank you.
Consider the following vectors:
?1 = (2, −1, 0)
?2 = (−1, 2, −1)
?3 = (0, −1, 2)
1. Use cbind to construct 3× 3 matrix which called ?.
2. Construct a matrix ? using the diagonal elements from point (3).
3. Calculate the upper triangular matrix using matrix ?.
4. Use outer function to divide each element in ?1 by the vector ?2.
5. Use outer function to add each element in ?1 to the vector ?2.
6. Use outer function to give the results of “each element in ?1 minus the elements of the vector ?2”.
R code with results:
> x1<-c(2, −1, 0)
> x2<-c(−1, 2, −1)
> x3<-c(0, −1, 2)
> m<-cbind(x1,x2,x3)
> m
x1 x2 x3
[1,] 2 -1 0
[2,] -1 2 -1
[3,] 0 -1 2
> u<-diag(diag(m),nrow = 3,ncol = 3)
> u
[,1] [,2] [,3]
[1,] 2 0 0
[2,] 0 2 0
[3,] 0 0 2
> x<-as.vector(c(-1,0,-1))
> x
[1] -1 0 -1
> y <- matrix(0, nrow=3, ncol=3)
> y[upper.tri(y)] <-x
> y
[,1] [,2] [,3]
[1,] 0 -1 0
[2,] 0 0 -1
[3,] 0 0 0
> z<-outer(x1,x2,FUN = "/")
> z
[,1] [,2] [,3]
[1,] -2 1.0 -2
[2,] 1 -0.5 1
[3,] 0 0.0 0
> w<-outer(x1,x2,FUN="+")
> w
[,1] [,2] [,3]
[1,] 1 4 1
[2,] -2 1 -2
[3,] -1 2 -1
> v<-outer(x1,x2,FUN="-")
> v
[,1] [,2] [,3]
[1,] 3 0 3
[2,] 0 -3 0
[3,] 1 -2 1
>
Get Answers For Free
Most questions answered within 1 hours.