Skip to content

Commit 5ee2569

Browse files
committed
Filled in the stubs
- Added code in makeCacheMatrix for creating a matrix object with setters and getters for matrix data and inverse - Added code in cacheSolve for calculating and setting the cache of the inverse of the matrix object created with makeCacheMatrix
1 parent 7f657dd commit 5ee2569

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
1+
## Creates a matrix object with setters and getters for
2+
## the matrix data itself as well as a cache for
3+
## computing its own inverse
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
i <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
i <<- NULL
10+
}
11+
get <- function() x
12+
setInverse <- function(inverse) i <<- inverse
13+
getInverse <- function() i
14+
list(set = set, get = get,
15+
setInverse = setInverse,
16+
getInverse = getInverse)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## Gets the inverse of a given matrix from its cache
21+
## or calculates it and stores it in the cache
1222

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
i <- x$getInverse()
25+
if(!is.null(i)) {
26+
message("getting cached data")
27+
return(i)
28+
}
29+
data <- x$get()
30+
i <- solve(data, ...)
31+
x$setInverse(i)
32+
i
1533
}

0 commit comments

Comments
 (0)