File tree Expand file tree Collapse file tree 1 file changed +28
-3
lines changed Expand file tree Collapse file tree 1 file changed +28
-3
lines changed Original file line number Diff line number Diff line change 6
6
# # same input matrix, the cached result will be returned, rather than the
7
7
# # inverse being calculated again.
8
8
9
+ # # makeCacheMatrix: accepts a matrix as input, and returns
10
+ # # an object with functions for getting and setting the cached matrix, as
11
+ # # well as getting and calculating/setting the inverse matrix.
9
12
10
13
makeCacheMatrix <- function (x = matrix ()) {
11
-
14
+ m <- NULL
15
+ set <- function (y ) {
16
+ x <<- y
17
+ m <<- NULL
18
+ }
19
+ get <- function () x
20
+ setinverse <- function (inverse ) m <<- inverse
21
+ getinverse <- function () m
22
+ list (set = set , get = get ,
23
+ setinverse = setinverse ,
24
+ getinverse = getinverse )
12
25
}
13
26
14
27
15
- # # Write a short comment describing this function
28
+ # # cacheSolve: accepts an object that was created via makeCacheMatrix, and
29
+ # # if the inverse of the matrix has previously calculated, returns that
30
+ # # cached result. Otherwise, the inverse of the matrix is calcuated, stored
31
+ # # in the object passed in to cacheSolve, and then the inverse matrix is
32
+ # # returned.
16
33
17
34
cacheSolve <- function (x , ... ) {
18
- # # Return a matrix that is the inverse of 'x'
35
+ m <- x $ getinverse()
36
+ if (! is.null(m )) {
37
+ message(" getting cached data" )
38
+ return (m )
39
+ }
40
+ data <- x $ get()
41
+ m <- solve(data , ... )
42
+ x $ setinverse(m )
43
+ m
19
44
}
You can’t perform that action at this time.
0 commit comments