forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
39 lines (30 loc) · 899 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
32
33
34
35
36
37
38
39
## caches the inverse of a matrix
makeCacheMatrix <- function(x = matrix()) {
im <- NULL
set <- function(y){
x <<- y
im <<- NULL
}
get <- function() x
##set and get inverse of matrix
setinverse <- function(inverse) im <<- inverse
getinverse <- function() im
##return a vector of list of functions possible on matrix
list( set = set, get = get, setinverse = setinverse, getinverse = getinverse)
}
## returns the inverse of a matrix 'x' from cache,
## or after computing it the first time
cacheSolve <- function(x, ...) {
## check if inverse was computed earlier
im <- x$getinverse()
if(!is.null(im)){
message("getting cached data")
return(im)
}
data <- x$get()
## get the inverse of given matrix
im <- solve(data,...)
##save the inverse
x$setinverse(im)
im
}