Skip to content

Commit 1414fd8

Browse files
committed
Written makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit 1414fd8

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

cachematrix.R

Lines changed: 32 additions & 6 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+
## These functions will compute the inverse of a given matrix
2+
## This is assuming the matrix is inversible
3+
## Once the inverse has been calculated it is stored in a cache
4+
## If the inverse is requested again, the cached value is returned
35

4-
## Write a short comment describing this function
56

6-
makeCacheMatrix <- function(x = matrix()) {
77

8+
## This function will create a special list containing functions
9+
## These functions can be used to set and get the matrix used
10+
## or to set and get the inverse of the matrix
11+
12+
makeCacheMatrix <- function(x = matrix()) {
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+
## This function uses the special list created by 'makeCacheMatrix'
28+
## It will check for a cached value of the inverse matrix and return it if found
29+
## If no cached value is found, calculate, set and return the inverse matrix
1230

1331
cacheSolve <- function(x, ...) {
14-
## 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)