Skip to content

Commit 492a8a3

Browse files
committed
Completed Assignment 2
1 parent 7f657dd commit 492a8a3

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

cachematrix.R

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Functions for caching inverse matrix calculation. This is solved with one
2+
## generator function which uses lexical scoping rules to store a cache for
3+
## inverse matrix. Before solving inverse matrix calculation, cache variable is
4+
## previously checked and if cache is different than NULL, cached value is
5+
## returned.
36

4-
## Write a short comment describing this function
7+
## Generator function for managing matrix values and inverse matrix cache.
58

69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
inv <- NULL
11+
set <- function(y) {
12+
x <<- y
13+
inv <<- NULL
14+
}
15+
get <- function() x
16+
setinverse <- function(inverse) inv <<- inverse
17+
getinverse <- function() inv
18+
list(set = set, get = get,
19+
setinverse = setinverse,
20+
getinverse = getinverse)
821
}
922

1023

11-
## Write a short comment describing this function
24+
## Function to solve inverse matrix calculation, while making use of caching
25+
## features provided by generator function.
1226

1327
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
28+
## Return a matrix that is the inverse of 'x'
29+
inv <- x$getinverse()
30+
if(!is.null(inv)) {
31+
message("getting cached result")
32+
return(inv)
33+
}
34+
data <- x$get()
35+
inv <- solve(data, ...)
36+
x$setinverse(inv)
37+
inv
1538
}

0 commit comments

Comments
 (0)