8000 Update cachematrix.R · susycote/ProgrammingAssignment2@abf76f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit abf76f7

Browse files
committed
Update cachematrix.R
Submission rdpeng#1
1 parent 7f657dd commit abf76f7

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

cachematrix.R

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions take a matrix as input and calculate it's inverse. The inverse is cached so that it can be accessed later
32

4-
## Write a short comment describing this function
3+
## makeCacheMatrix takes a matrix as input and calculates the inverse. The inverse and matrix itself are outputs to a list.
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
inverseCache <- NULL
7+
get <- function() {x}
8+
setinverse <- function(solve)
9+
{inverseCache <<- solve}
10+
getinverse <- function() {inverseCache}
11+
list(get = get , setinverse = setinverse , getinverse = getinverse)
812
}
913

1014

11-
## Write a short comment describing this function
15+
## cacheSolve returns the inverse of a matrix. It first checks to see if the inverse has already been calculated. If so, it returns the cached inverse. If not, it calculates the inverse and returns it, storing the inversion in the same object as makeCacheMatrix.
1216

17+
## Return a matrix that is the inverse of 'x'
1318
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
19+
inverseCache <- x$getinverse
20+
if(!is.null(inverseCache)){
21+
message('getting cached matrix inversion')
22+
return(inverseCache)
23+
}
24+
matrixdata <- x$get()
25+
inverseCache <- solve(matrixdata)
26+
x$setinverse(inverseCache)
27+
inverseCache
1528
}

0 commit comments

Comments
 (0)
0