Skip to content

Commit 4a9c367

Browse files
committed
Assignment solution functions
1 parent 5813265 commit 4a9c367

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

cachematrix.R

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
## This is the solution to the ProgrammingAssignment 2 for Peer Evaluation
2-
## Put comments here that give an overall description of what your
3-
## functions do
42

5-
## Write a short comment describing this function
3+
## Creates a matric-inverse object which can be cached to reduce the overhead of calculation every time its required
4+
5+
## Usage Example : M <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
6+
## cacheMatrix <- makeCacheMatrix(M)
7+
## cacheSolve(cacheMatrix)
8+
9+
## Create a cacheMatrix object
610

711
makeCacheMatrix <- function(x = matrix()) {
12+
cachedInverse <- NULL
13+
set <- function(y) {
14+
x <<- y
15+
cachedInverse <<- NULL
16+
}
17+
get <- function() x
18+
setInverse <- function(inverse) cachedInverse <<- inverse
19+
getInverse <- function() cachedInverse
20+
list(set = set, get = get,
21+
setInverse = setInverse,
22+
getInverse = getInverse)
823

924
}
1025

@@ -13,4 +28,13 @@ makeCacheMatrix <- function(x = matrix()) {
1328

1429
cacheSolve <- function(x, ...) {
1530
## Return a matrix that is the inverse of 'x'
31+
invFunc <- x$getInverse()
32+
if(!is.null(invFunc)) {
33+
message("getting cached data")
34+
return(invFunc)
35+
}
36+
data <- x$get()
37+
invFunc <- solve(data, ...)
38+
x$setInverse(invFunc)
39+
invFunc
1640
}

0 commit comments

Comments
 (0)