forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
40 lines (35 loc) · 1.48 KB
/
cachematrix.R
File metadata and controls
40 lines (35 loc) · 1.48 KB
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
##The function makeCacheMatrix defines a matrix and caches
##its inverse. The second function, cacheSolve, either returns
##a cached inverse or calculates it and then reports the value.
## This function creates a matrix that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
#this first part sets the value of the matrix
#the inverse is NULL in two different environments to clear the cache
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
#now defining function to set value of the matrix
get <- function() x
#now setting the inverse
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
}
## This function calculates the inverse of the matrix from above
#and then checkes to see whether the inverse has already been
#calculated. If so, then it retrieves the inverse from the cache.
cacheSolve <- function(x, ...) {
inv <- x$getinverse() #gets inverse from cache
if(!is.null(inv)) {
message("getting your inverse from within cyberspace")
return(inv)
}
#but if there's nothing in the cache, we have to calculate
#the inverse, put it in the cache, and get the value.
matrixvalue <- x$get()
inv <- solve(matrixvalue) #take the inverse
x$setinverse(inv)
inv
}