forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
46 lines (35 loc) · 1.16 KB
/
cachematrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## Put comments here that give an overall description of what your
## functions do
## First create the function to invert or retrieve
makeCacheMatrix <- function(x = matrix()) {
INV <- NULL #not sure why I need this but it seems I do
set <- function(y) {
x <<- y
INV <<- NULL #overwrites any value of INV in the cache
}
get <- function() x #not defined here so will come from parent enviro
setinverse <- function(inverse) INV <<- inverse #inputs value of inverse to INV in parent
getinverse <- function() INV #retrieves from parent enviro
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Then invert the matrix, retrieving from cache if available
cacheSolve <- function(x, ...) {
INV <- x$getinverse()
if(!is.null(INV)) {
message("getting cached data")
return(INV)
}
matrix <- x$get()
INV <- solve(matrix, ...)
x$setinverse(INV)
INV
}
## Testing the code to see if it works
m <- matrix(c(6,4,8,9,1,2,10,3,8), nrow=3, ncol=3)
cm <- makeCacheMatrix(m)
a <- cacheSolve(cm)
a
#then multiply to check for identity
round(a %*% m, 1)