forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
31 lines (23 loc) · 919 Bytes
/
cachematrix.R
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
# makeCacheMatrix: This function takes a matrix and creates a list with 4 functions:
# reset the matrix, get the matrix, set the inverse and get the invere (if it has been stored)
makeCacheMatrix <- function(m1 = matrix(rnorm(4),2,2) ) {
inv<-NULL
set<-function(m2) {m1<<-m2; inv<<-NULL}
get <- function() m1
setinv <- function(m3) inv <<- m3
getinv <- function() inv
list(set=set,get=get,setinv=setinv,getinv=getinv)
}
# cacheSolve: This function computes the inverse of the special "matrix"
# returned by makeCacheMatrix above. If the inverse is stored, then cacheSolve
# should retrieve the inverse from the cache.
cacheSolve <- function(lm1, ...) {
inv <- lm1$getinv()
if(!is.null(inv)) {message("Getting inverse from cache")}
else {
message("Inverse has been calculated")
inv <- solve(lm1$get())
lm1$setinv(inv)
}
inv
}