-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module 6
54 lines (48 loc) · 1.21 KB
/
Module 6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1. Consider A=matrix(c(2,0,1,3), ncol=2) and B=matrix(c(5,2,4,-1), ncol=2).
a) Find A + B
b) Find A - B
> A=matrix(c(2,0,1,3),ncol=2)
> B=matrix(c(5,2,4,-1),ncol=2)
> A-B
[,1] [,2]
[1,] -3 -3
[2,] -2 4
> A+B
[,1] [,2]
[1,] 7 5
[2,] 2 2
(This concludes question 1)
2. Using the diag() function to build a matrix of size 4 with the following values in the diagonal 4,1,2,3.
> diag(c(4,1,2,3), ncol=4)
[,1] [,2] [,3] [,4]
[1,] 4 0 0 0
[2,] 0 1 0 0
[3,] 0 0 2 0
[4,] 0 0 0 3
(This concludes question 2)
generate the following matrix:
## [,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
> diag(x=3, nrow=5, ncol=5)
[,1] [,2] [,3] [,4] [,5]
[1,] 3 0 0 0 0
[2,] 0 3 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 3 0
[5,] 0 0 0 0 3
> m <-diag(x=3, ncol=5, nrow=5)
> sweep(m,1,c(1),"+")
[,1] [,2] [,3] [,4] [,5]
[1,] 4 1 1 1 1
[2,] 1 4 1 1 1
[3,] 1 1 4 1 1
[4,] 1 1 1 4 1
[5,] 1 1 1 1 4
> m[1, ] + 1
[1] 4 1 1 1 1
> m[,1] + 2
[1] 5 2 2 2 2