Skip to content

Commit 49a8600

Browse files
committed
implemented cachematrix
1 parent 7f657dd commit 49a8600

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

cachematrix.R

Lines changed: 26 additions & 9 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
1+
## makeCacheMatrix and cacheSolve allow you to calculate the inverse
2+
## of a matrix and cache the result
53

4+
## Create a data structure containing a matrix and, optionally, its inverse
5+
## and return getters and setters for both.
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
xinverse <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
xinverse <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) xinverse <<- inverse
14+
getinverse <- function() xinverse
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
818
}
919

1020

11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
21+
## Given a CacheMatrix return or compute and cache the inverse of the matrix.
22+
cacheSolve <- function(x) {
23+
xinverse <- x$getinverse()
24+
if(!is.null(xinverse)) {
25+
message("getting cached data")
26+
return(xinverse)
27+
}
28+
matrix <- x$get()
29+
xinverse <- solve(matrix)
30+
x$setinverse(xinverse)
31+
xinverse
1532
}

0 commit comments

Comments
 (0)