In: Statistics and Probability
A =
(1 −7 5 0
0 10 8 2
2 4 10 3
−4 8 −9 6)
(1) Count the number of rows that contain negative
components.
(2) Obtain the inverse of A and count the number of columns that
contain even number of positive components.
(3) Assign column names (a,b,c,d) to the columns of A.
(4) Transform the matrix A into a vector object a by stacking
rows.
(5) Replace the diagonal components of A with (0,0,2,3). Hint: use
a “diag” function
> A =
matrix(c(1,0,2,-4,-7,10,4,8,5,8,10,-9,0,2,3,6),ncol=4,nrow=4);A
[,1] [,2] [,3] [,4]
[1,] 1 -7 5 0
[2,] 0 10 8 2
[3,] 2 4 10 3
[4,] -4 8 -9 6
1) number of rows that contain negative components
--
> B = A[-2:-3, ]
> B
[,1] [,2] [,3] [,4]
[1,] 1 -7 5 0
[2,] -4 8 -9 6
> nrow(B)
2
2) number of columns that contain even number of positive
components --
inverse = solve(A);inverse
[,1] [,2] [,3] [,4]
[1,] -0.49921753 -0.4765258 0.53364632 -0.107981221
[2,] -0.11580595 0.0258216
0.02034429 -0.018779343
[3,] 0.13771518 0.1314554 -0.07824726 -0.004694836
[4,] 0.02816901 -0.1549296 0.21126761
0.112676056
> count = inverse[ ,1:3]
> ncol(count)
3
3) Assign column names (a,b,c,d) to the columns of A
--
> colnames(A)=c('a','b','c','d')
> A
a b c d
[1,] 1 -7 5 0
[2,] 0 10 8 2
[3,] 2 4 10 3
[4,] -4 8 -9 6
4) matrix A into a vector object a by stacking rows
--
> as.vector(A)
[1] 1 0 2 -4 -7 10 4 8 5 8 10 -9 0 2 3 6
5) Replace the diagonal components of A with (0,0,2,3)
--
> diag(A)<-c(0,0,2,4)
> A
a b c d
[1,] 0 -7 5 0
[2,] 0 0 8 2
[3,] 2 4 2 3
[4,] -4 8 -9 4