Skip to content

Commit 9552695

Browse files
committed
added makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit 9552695

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

cachematrix.R

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These two functions are used to effeciently and with caching
2+
## facilitate the return of the inverse of a square matrix
3+
## a list of set and get functions are used to accomplish this
34

4-
## Write a short comment describing this function
5+
## makeCacheMatrix takes in a square matrix and sets up a list that gives
6+
## access to set or get the matrix and its inverse
7+
## this list is a special matrix
58

69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
i <- NULL
11+
set <- function(y) {
12+
x <<- y
13+
i <<- NULL
14+
}
15+
get <- function() x
16+
setinverse <- function(inverse) i <<- inverse
17+
getinverse <- function() i
18+
list(set = set, get = get,
19+
setinverse = setinverse,
20+
getinverse = getinverse)
821
}
922

1023

11-
## Write a short comment describing this function
24+
## cacheSolve takes in a special matrix from makeCacheMatrix and using its
25+
## functions sets the inverse and saves it to cache or gets it from cache
1226

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

0 commit comments

Comments
 (0)