File tree Expand file tree Collapse file tree 1 file changed +48
-5
lines changed Expand file tree Collapse file tree 1 file changed +48
-5
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
1
4
- # # Write a short comment describing this function
2
+
3
+ # # This function creates a special "matrix" object that can cache its inverse.
5
4
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
6
7
+ m <- NULL
8
+
9
+ set <- function (y ) {
10
+ x <<- y
11
+ m <<- NULL
12
+ }
13
+ get <- function () x
14
+
15
+ setmatrix <- function (matrix ) m <<- matrix
16
+ getmatrix <- function () m
17
+
18
+ list (set = set , get = get ,
19
+ setmatrix = setmatrix ,
20
+ getmatrix = getmatrix )
21
+
8
22
}
9
23
10
24
11
- # # Write a short comment describing this function
25
+ # # This function computes the inverse of the special
26
+ # # "matrix" returned by `makeCacheMatrix` above. If the inverse has
27
+ # # already been calculated (and the matrix has not changed), then
28
+ # # `cacheSolve` should retrieve the inverse from the cache.
12
29
13
30
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
31
+
32
+ # # Return a matrix that is the inverse of 'x'
33
+
34
+ m <- x $ getmatrix()
35
+ if (! is.null(m )) {
36
+ message(" getting cached data" )
37
+ return (m )
38
+ }
39
+ data <- x $ get()
40
+ m <- solve(data , ... )
41
+ x $ setmatrix(m )
42
+ m
43
+
15
44
}
45
+
46
+ # steps to test
47
+
48
+ # load makeCacheMatrix and cacheSolve
49
+
50
+ # m <- matrix(
51
+ # c(3, 1, 5, 7),
52
+ # nrow=2,
53
+ # ncol=2,
54
+ # byrow = TRUE)
55
+
56
+ # mc <- makeCacheMatrix(m)
57
+
58
+ # cacheSolve(mc)
You can’t perform that action at this time.
0 commit comments