Skip to content

Commit 81c53e1

Browse files
committed
Submitting solution
1 parent 7f657dd commit 81c53e1

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

cachematrix.R

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
61
makeCacheMatrix <- function(x = matrix()) {
7-
2+
#This function creates a special "matrix" object that can cache its inverse.
3+
inverse<-NULL
4+
set <- function(y){
5+
x<<-y
6+
inverse<<-NULL
7+
}
8+
get <- function() x
9+
setinverse <- function(inv) inverse<<-inv
10+
getinverse <- function() inverse
11+
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
812
}
913

1014

11-
## Write a short comment describing this function
1215

1316
cacheSolve <- function(x, ...) {
1417
## Return a matrix that is the inverse of 'x'
18+
# This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
19+
#If the inverse has already been calculated (and the matrix has not changed),
20+
# then the cachesolve should retrieve the inverse from the cache.
21+
inverse<-x$getinverse()
22+
if(!is.null(inverse)){
23+
message("getting cached data")
24+
return(inverse)
25+
}
26+
data<-x$get()
27+
inverse<-solve(data, ...)
28+
#ellipses in case we want to pass additional arguments to solve()
29+
#note the ellipses in the function def that implies this functionality
30+
#is desired
31+
x$setinverse(inverse) #set it for caching
32+
inverse #return
1533
}

0 commit comments

Comments
 (0)