Skip to content

Commit d8cda42

Browse files
committed
Programming Assisgnment 2
Completed code for cachematrix programming assignment2
1 parent 7f657dd commit d8cda42

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

cachematrix.R

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## below are the functions that cache and compute the inverse of
2+
## a matrix
33

4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
4+
## This function creates special "matrix" object that can cache its
5+
## inverse
76

7+
makeCacheMatrix <- function(mtx = matrix()) {
8+
inverse <- NULL
9+
set <- function(x) {
10+
mtx <<- x;
11+
inverse <<- NULL;
12+
}
13+
get <- function() return(mtx);
14+
setinv <- function(inv) inverse <<- inv;
15+
getinv <- function() return(inverse);
16+
return(list(set = set, get = get, setinv = setinv, getinv = getinv))
817
}
918

1019

11-
## Write a short comment describing this function
20+
## This function computes the inverse of the special "matrix"
21+
##returned by makecacheMatrix above. If inverse has been calculated
22+
## already, then cacheSolve Should retrieve the inverse the
23+
## from the cache
1224

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
cacheSolve <- function(mtx, ...) {
26+
inverse <- mtx$getinv()
27+
if(!is.null(inverse)) {
28+
message("Getting cached data...")
29+
return(inverse)
30+
}
31+
data <- mtx$get()
32+
invserse <- solve(data, ...)
33+
mtx$setinv(inverse)
34+
return(inverse)
1535
}

0 commit comments

Comments
 (0)