Skip to content

Commit 6567c75

Browse files
author
Chowdhury Yeameen
committed
Submitting Assignment2 solution
1 parent 7f657dd commit 6567c75

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

cachematrix.R

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
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 creates a special matrix that can keep the
2+
## original matrix and inverse of the matrix (if set)
3+
## cacheSolve receives an instance of makeCacheMatrix and
4+
## checks if inverse is available in cache. If not, computes the inverse,
5+
## saves to the cache, and return the result
56

7+
## This function wraps a matrix and returns a list of
8+
## operation to set and get the matrix and it's inverse matrix
69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
inverseMatrix <- NULL
11+
set <- function(inputMatrix) {
12+
x <<- inputMatrix
13+
inverseMatrix <<- NULL
14+
}
15+
get <- function() x
16+
setInverse <- function(inverseM) inverseMatrix <<- inverseM
17+
getInverse <- function() {
18+
inverseMatrix
19+
}
20+
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
821
}
922

1023

11-
## Write a short comment describing this function
24+
25+
## This function returns the inverse of matrix stored in parameter
26+
## It first checks for cache, if not availalbe, computes the cache.
27+
## Otherwise just returns from the cache.
1228

1329
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
30+
31+
inverseMatrix <- x$getInverse()
32+
33+
if(!is.null(inverseMatrix)) { ## Found in cache
34+
return(inverseMatrix)
35+
}
36+
37+
m <- x$get()
38+
inverseMatrix <- solve(m)
39+
x$setInverse(inverseMatrix)
40+
inverseMatrix
1541
}

0 commit comments

Comments
 (0)