Skip to content

Commit f6c6984

Browse files
committed
Homework Assignment 2
1 parent 7f657dd commit f6c6984

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

cachematrix.R

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
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+
## This pair of functions allow the use of lexical scoping to hold state i.e cache
2+
## results
53

4+
## creates a special "matrix", which is really a list containing 4 function to
5+
## set and get the function values and set and get the inverse of the input matrix
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(inv) i <<- inv
14+
getinv <- function() i
15+
list(set = set, get = get,
16+
setinv = setinv,
17+
getinv = getinv)
18+
819
}
920

1021

11-
## Write a short comment describing this function
12-
22+
## This function takes the output of the 'makeCacheMatrix' function as input
23+
## and either gets a cached value or "solves" the inverse and stores that in the cache
1324
cacheSolve <- function(x, ...) {
1425
## Return a matrix that is the inverse of 'x'
26+
i <- x$getinv()
27+
if(!is.null(i)) {
28+
message("getting cached data")
29+
return(i)
30+
}
31+
data <- x$get()
32+
i <- solve(data) ##%*% data
33+
x$setinv(i)
34+
i
35+
1536
}

0 commit comments

Comments
 (0)