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 (27 loc) · 801 Bytes
/
cachematrix.R
File metadata and controls
32 lines (27 loc) · 801 Bytes
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
## These functions cache the inverse of a matrix.
## This function creates a special matrix, with setters and getters
## for its own value, and its inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y){
x <<- y
inv <<- NULL
}
get <- function() x
setsolve <- function(s) inv <<- s
getsolve <- function() inv
list(set=set,get=get,setsolve=setsolve,getsolve=getsolve)
}
## This function returns the inverse of a matrix. If the inverse of the
## given matrix is already cached, return the cached inverse without
## repetitive computation. Otherwise, compute the inverse and cache it.
cacheSolve <- function(x, ...) {
inv <- x$getsolve()
if (!is.null(inv)){
message("getting cached inverse")
return(inv)
}
inv <- solve(x$get())
x$setsolve(inv)
inv
}