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
32 lines (30 loc) · 1.38 KB
/
Copy pathcachematrix.R
File metadata and controls
32 lines (30 loc) · 1.38 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
# the following two functions are used to cache the inverse of a matrix
# makeCacheMatrix creates a special "matrix" object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
i <- NULL # i is the inverse
set <- function(y) { # to set the data
x <<- y
i <<- NULL
}
get <- function() x # to get the data
setinverse <- function(inverse) i <<- inverse # to set the inverse
getinverse <- function() i # to get the inverse
list(set = set, get = get, # return a list of functions
setinverse = setinverse,
getinverse = getinverse)
}
# cacheSolve computes the inverse of the special "matrix" returned by
# makeCacheMatrix above. If the inverse has already been calculated
# (and the matrix has not changed), then the cachesolve should retrieve
# the inverse from the cache.
cacheSolve <- function(x, ...) {
i <- x$getinverse() # get inverse
if(!is.null(i)) { # check if there is cached inverse
message("getting cached data")
return(i) # if there is cached inverse, return the inverse
}
data <- x$get() # import the data
i <- solve(data, ...) # calculate the inverse
x$setinverse(i) # set the inverse
i # return the inverse
}