Skip to content

Commit c8efe11

Browse files
committed
wrote functions makeCacheMatrix and cacheSolve
1 parent 7f657dd commit c8efe11

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

cachematrix.R

Lines changed: 24 additions & 7 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
3-
4-
## Write a short comment describing this function
51

2+
## Return a matrix container object that can cache its inverse.
3+
## Use with a caching function.
64
makeCacheMatrix <- function(x = matrix()) {
7-
5+
i <- NULL
6+
set <- function(y) {
7+
x <<- y
8+
i <<- NULL
9+
}
10+
get <- function() x
11+
setinverse <- function(inverse) i <<- inverse
12+
getinverse <- function() i
13+
list(set = set, get = get,
14+
setinverse = setinverse,
15+
getinverse = getinverse)
816
}
917

1018

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

20+
## Return a matrix inverse from a matrix container.
21+
## Function caches the inverse in the container.
1322
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
23+
i <- x$getinverse()
24+
if(!is.null(i)) {
25+
message("getting cached inverse")
26+
return(i)
27+
}
28+
data <- x$get()
29+
i <- solve(data)
30+
x$setinverse(i)
31+
i
1532
}

0 commit comments

Comments
 (0)