Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2762b71

Browse files
unknownunknown
unknown
authored and
unknown
committed
Caching the Inverse of a Matrix functions
1 parent 7f657dd commit 2762b71

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

cachematrix.R

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## The matrix inversion is usualy a costly operation.
2+
## The set of functions below intended to cache matrix inverse
3+
## to save computational time on inverse calculartion.
54

5+
## This function creates a special "matrix" object that can cache its inverse.
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
i <<- NULL
11+
}
12+
get <- function() x
13+
setinv <- function(inverse) i <<- inverse
14+
getinv <- function() i
15+
list(set = set, get = get, setinv = setinv, getinv = getinv)
816
}
917

1018

11-
## Write a short comment describing this function
12-
19+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
20+
## If the inverse has already been calculated, then the cachesolve should retrieve the inverse from the cache
1321
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
22+
## Return a matrix that is the inverse of 'x'
23+
i <- x$getinv()
24+
if (!is.null(i)) {
25+
return(i)
26+
}
27+
data <- x$get()
28+
inverse <- solve(data, ...)
29+
x$setinv(inverse)
30+
inverse
1531
}

0 commit comments

Comments
 (0)