Skip to content

Commit ac4f353

Browse files
committed
Added the code for the two functions.
1 parent 7f657dd commit ac4f353

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

cachematrix.R

Lines changed: 30 additions & 4 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+
## The two functions here make use of environments to cache results that
2+
## are expensive to compute and are used many times.
3+
## Here the inversion of a matrix is acheived using caching.
4+
## note that the matrix is considered invertible.
35

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

7+
## function makeCacheMatrix, takes a regular matrix
8+
## and creates a special type of matrix, that includes setters and getters
9+
## and a cache variable that stores the inverse
610
makeCacheMatrix <- function(x = matrix()) {
11+
minv <- NULL
12+
set <- function(y){
13+
x <<- y
14+
m <<- NULL
15+
}
16+
get <- function() x
17+
setinv <- function(solve) minv <<-solve
18+
getinv <- function() minv
19+
list(set=set,get=get,setinv=setinv,getinv=getinv)
720

821
}
922

1023

11-
## Write a short comment describing this function
24+
## function cacheSolve checks if the inverse for this matrix is already
25+
## available in the cache, if so, it returns the cached inverse
26+
## if its not available it computes the inverse, stores it in the cache
27+
## and then returns the inverse
1228

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

0 commit comments

Comments
 (0)