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
43 lines (35 loc) · 1.02 KB
/
cachematrix.R
File metadata and controls
43 lines (35 loc) · 1.02 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
41
42
43
## Functions that will create and cache matrix inverse objects for lookup at
## future computations
## Function creates a matrix object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
#stores inverse matrix
inverse <- NULL
#sets the matrix
set <- function(y) {
x <<- y
inverse <<- NULL
}
#gets the matrix
get <- function() x
#sets the inverse
setinverse <- function(solve) inverse <<- solve
#gets the inverse
getinverse <- function() inverse
#returns list with all new functions
list(set = set, get = get, setinverse = setinverse, getinverse=getinverse)
}
## Function searching for cached inverse and otherwise making inverse and storing
## in cache
cacheSolve <- function(x, ...) {
inverse <- x$getinverse()
# If the inverse is in cache, return it
if(!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
#otherwise calculate and cache the inverse
data <- x$get()
inverse <- solve(data, ...)
x$setinverse(inverse)
inverse
}