forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
43 lines (32 loc) · 1.93 KB
/
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
40
41
42
43
## The following functions speed up to compute repeated matrix inversion
## for a single matrix using caching technique
## This function creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
im <- NULL ## im for inversion matrix
set <- function(y) { ## set the matrix
x <<- y
im <<- NULL
}
get <- function() x ## get the matrix
setinv <- function(solve) im <<- solve ## set inversion matrix
getinv <- function() im ## get inversion matrix
list(set = set, ## store the functions
get = get,
setinv = setinv,
getinv = getinv)
}
## This function 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, ...) {
## Return a matrix that is the inverse of 'x'
im <- x$getinv() ## get inversion from cache
if(!is.null(im)){ ## check if it is not NULL
message("getting cached data")
return(im) ## return the cache data and save some computation
}
data <- x$get() ## if we are here we need compute
im <- solve(data, ...) ## compute the inverse
x$setinv(im) ## save it to cache
im ## return the computed inverse matrix
}