Skip to content

Commit ac01380

Browse files
author
Eugene Ray
committed
changes for assignment
1 parent 7f657dd commit ac01380

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

cachematrix.R

Lines changed: 27 additions & 7 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+
## makeCacheMatrix creates a new matrix object that can store its own inverse
2+
## cacheSolve returnes cached inverse of matrix produced by makeCacheMatrix or
3+
## calculates and caches it if the inverse wasn't calculated before
34

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

6+
## create a special matrix that can store cache of its inverse
67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inv <- NULL
9+
set <- function(y,...) {
10+
x <<- matrix(y,...)
11+
inv <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(inverse) inv <<- inverse
15+
getinverse <- function() inv
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
819
}
920

1021

11-
## Write a short comment describing this function
12-
22+
## try to retrieve cached inverse matrix,
23+
## if not avaliable, then calculate, cache and return
1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
## Return a matrix that is the inverse of 'x'
26+
inv <- x$getinverse()
27+
if(!is.null(inv)) {
28+
message("getting cached data")
29+
return(inv)
30+
}
31+
matrix <- x$get()
32+
inv <- solve(matrix, ...)
33+
x$setinverse(inv)
34+
inv
1535
}

0 commit comments

Comments
 (0)