Skip to content

Commit 4763f90

Browse files
committed
Initial assignment submission
1 parent 7f657dd commit 4763f90

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

cachematrix.R

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Given an invertible matrix compute the inverse and cache the result. Subsequent requests to get the inverse
2+
## of the same matrix will return the cache result instead of re-calculating.
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+
i <<- NULL
11+
}
12+
get <- function() x
13+
setinv <- function(inv) i <<- inv
14+
getinv <- function() i
15+
list(set = set, get = get, setinv = setinv, getinv = getinv)
816
}
917

1018

11-
## Write a short comment describing this function
19+
## This function computes and caches the inverse of the special "matrix" returned by makeCacheMatrix above.
1220

1321
cacheSolve <- function(x, ...) {
1422
## Return a matrix that is the inverse of 'x'
23+
i <- x$getinv()
24+
if(!is.null(i)) {
25+
message("getting cached data")
26+
return(i)
27+
}
28+
data <- x$get()
29+
i <- solve(data, ...)
30+
x$setinv(i)
31+
i
1532
}

0 commit comments

Comments
 (0)