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

Skip to content

Commit 464773d

Browse files
committed
Implement makeCacheMatrix and cacheSolve
1 parent 7f657dd commit 464773d

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

cachematrix.R

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## makeCacheMatrix: This function creates a special "matrix" object that
2+
## can cache its inverse.
3+
##
4+
## cacheSolve: This function computes the inverse of the special
5+
## "matrix" returned by makeCacheMatrix above. If the inverse has
6+
## already been calculated (and the matrix has not changed), then the
7+
## cachesolve should retrieve the inverse from the cache.
38

4-
## Write a short comment describing this function
9+
## makeCacheMatrix creates a special "matrix", which is really a list
10+
## containing functions to get/set the matrix and its inverse
511

612
makeCacheMatrix <- function(x = matrix()) {
7-
13+
i <- NULL
14+
set <- function(y) {
15+
x <<- y
16+
i <<- NULL
17+
}
18+
get <- function() x
19+
setinverse <- function(inverse) i <<- inverse
20+
getinverse <- function() i
21+
list(set = set, get = get,
22+
setinverse = setinverse,
23+
getinverse = getinverse)
824
}
925

1026

11-
## Write a short comment describing this function
27+
## cacheSolve returns an inverse of the given matrix, utilising cache
28+
## mechanism provided by makeCacheMatrix
1229

1330
cacheSolve <- function(x, ...) {
1431
## Return a matrix that is the inverse of 'x'
32+
i <- x$getinverse()
33+
if(!is.null(i)) {
34+
message("getting cached data")
35+
return(i)
36+
}
37+
data <- x$get()
38+
i <- solve(data, ...)
39+
x$setinverse(i)
40+
i
1541
}

0 commit comments

Comments
 (0)