Skip to content

Commit 1a3c727

Browse files
unknownunknown
authored andcommitted
complete the functions
1 parent e4eed41 commit 1a3c727

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cachematrix.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
# Matrix inversion is usually a costly computation and their may be some benefit
2+
# to caching the inverse of a matrix rather than compute it repeatedly
33

4-
## Write a short comment describing this function
4+
# This function creates a special "matrix" object that can cache its inverse.
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) i <<- inverse
14+
getinverse <- function() i
15+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
816
}
917

1018

11-
## Write a short comment describing this function
19+
# This function computes the inverse of the special "matrix" returned by
20+
# makeCacheMatrix above. If the inverse has already been calculated (and the
21+
# matrix has not changed), then the cachesolve should retrieve the inverse from
22+
# the cache.
1223

1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
## Return a matrix that is the inverse of 'x'
26+
i <- x$getinverse()
27+
if (!is.null(i)) {
28+
message("gettting cached data")
29+
return(i)
30+
}
31+
32+
data <- x$get()
33+
i <- solve(data, ...)
34+
x$setinverse(i)
35+
i
1536
}

0 commit comments

Comments
 (0)