Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55c071d

Browse files
committed
Tested successfully
1 parent 7f657dd commit 55c071d

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

cachematrix.R

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## The following functions are used in tandom to create a matrix along with calculating and
2+
## caching its inverse (a time consuming operation).
3+
##
4+
## Example usage
5+
## a<-makeCacheMatrix()
6+
## a$set(matrix(1:4,2,2))
7+
## cacheSolve(a)
38

4-
## Write a short comment describing this function
9+
## makeCacheMatrix takes a matrix as an argument, calls cacheSolve() to calculate and
10+
## store the inverse of the matrix in cache. The method also contains functions to get the matrix and
11+
## to get and set the inverse of the matrix.
512

613
makeCacheMatrix <- function(x = matrix()) {
7-
14+
inverseMatrix <- NULL
15+
set <- function(y) {
16+
x <<- y
17+
inverseMatrix <<- NULL
18+
}
19+
get <- function() x
20+
setInverseMatrix <- function(solve) inverseMatrix <<- solve
21+
getInverseMatrix <- function() inverseMatrix
22+
list(set = set, get = get,
23+
setInverseMatrix = setInverseMatrix,
24+
getInverseMatrix = getInverseMatrix)
825
}
926

1027

11-
## Write a short comment describing this function
28+
## cacheSolve takes in a matrix as an argument and returns the cached inverse if it exists,
29+
## otherwise it calculates the inverse, stores it in cache and then returns it.
1230

13-
cacheSolve <- function(x, ...) {
31+
cacheSolve <- function(x =, ...) {
1432
## Return a matrix that is the inverse of 'x'
33+
34+
inverseMatrix <- x$getInverseMatrix()
35+
if(!is.null(inverseMatrix)) {
36+
message("getting cached data")
37+
return(inverseMatrix)
38+
}
39+
data <- x$get()
40+
inverseMatrix <- solve(data, ...)
41+
x$setInverseMatrix(inverseMatrix)
42+
inverseMatrix
1543
}

0 commit comments

Comments
 (0)