-
Notifications
You must be signed in to change notification settings - Fork 0
/
M6HW.R
70 lines (57 loc) · 1.52 KB
/
M6HW.R
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
##################################################
# ------------LIS_4370-M6_Assignment---------- #
##################################################
#' ---
#' title: "Doing Math Part 2"
#' author: "Kevin Hitt"
#' date: "February 23rd, 2020"
#' ---
# 1. Consider A = matrix(c(2,0,1,3), ncol=2) and B = matrix(c(5,2,4,-1), ncol=2)
A = matrix(c(2,0,1,3), ncol=2)
B = matrix(c(5,2,4,-1), ncol=2)
# > A
# [,1] [,2]
# [1,] 2 1
# [2,] 0 3
#
# > B
# [,1] [,2]
# [1,] 5 4
# [2,] 2 -1
# a) Find A + B
A + B
# [,1] [,2]
# [1,] 7 5
# [2,] 2 2
# b) Find A - B
A - B
# [,1] [,2]
# [1,] -3 -3
# [2,] -2 4
# 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), 4, 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
# 3. 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
# Hint: You can use the command diag() to build it.
C <- diag(c(3), 5, 5)
C[,1] <- C[,1] + 2
C[1,] <- C[1,] + 1
C[1,1] <- 3
# [,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