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
53 lines (43 loc) · 1.2 KB
/
Copy pathcachematrix.R
File metadata and controls
53 lines (43 loc) · 1.2 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
44
45
46
47
48
49
50
51
52
53
## This function caches the inverse of a matrix
## using the solve function in R.
## This function constructs the cache as a list
## of four functions, allowing the user to set
## or get the matrix, as well as set or get the
## inverse of the matrix.
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() {
x
}
setinverse <- function(inverse) {
i <<- inverse
}
getinverse <- function() {
i
}
return(list(set = set, get = get, setinverse = setinverse, getinverse = getinverse))
}
## This function allows for the inverse
## of the matrix to be calculated using
## the solve function. However, before
## doing so, it checks the cache to see
## if the inverse has already been
## calculated. If it has, the function
## returns the already calculated
## inverse. If not, it calculates the
## inverse itself using solve().
cacheSolve <- function(x, ...) {
i <- x$getinverse()
if(!is.null(i)){
message("getting cached data...")
return(i)
}
matrix <- x$get()
i <- solve(matrix, ...)
x$setinverse(i)
return(i)
}