Skip to content

Commit 56f84c8

Browse files
author
root
committed
Matrix Inversion Caching Code Added
1 parent 7f657dd commit 56f84c8

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

cachematrix.R

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
##
2+
## Matrix Inversion Caching
3+
###
34

4-
## Write a short comment describing this function
5+
## Creates a special "matrix" object that can cache its inverse.
56

6-
makeCacheMatrix <- function(x = matrix()) {
7+
makeCacheMatrix <- function(m = matrix()) {
8+
i <- NULL
9+
set <- function(v){
10+
m <<- v
11+
i <<- NULL
12+
}
13+
get <- function() m
14+
setinverse <- function(inverse) i <<- inverse
15+
getinverse <- function() i
16+
list(set= set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
719

820
}
921

1022

11-
## Write a short comment describing this function
23+
## Computes the inverse.
24+
## If the inverse is already computed,
25+
## then the cachesolve should retrieve the inverse from the cach
1226

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
cacheSolve <- function(m, ...) {
28+
i <- m$getinverse()
29+
if (!is.null(i)){
30+
message("getting cached data")
31+
return(i)
32+
}
33+
data <- m$get()
34+
i <- solve(data, ...)
35+
m$setinverse(i)
36+
i
1537
}
38+

0 commit comments

Comments
 (0)