Skip to content

Commit 4aa62e5

Browse files
Implement makeCacheMatrix and cacheSolve
1 parent 7f657dd commit 4aa62e5

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

cachematrix.R

Lines changed: 26 additions & 6 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+
## A pair of functions that cache the inverse of a matrix.
32

4-
## Write a short comment describing this function
3+
## Create a special "matrix" object that can cache its inverse.
4+
##
5+
## Returns list of functions "set", "get", "setinv", "getinv".
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inv <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
inv <<- NULL
12+
}
13+
get <- function() x
14+
setinv <- function(x) inv <<- x
15+
getinv <- function() inv
16+
list(set = set, get = get,
17+
setinv = setinv,
18+
getinv = getinv)
819
}
920

1021

11-
## Write a short comment describing this function
22+
## The following function calculates the mean of the special "vector" created with the above function.
23+
##
24+
## Return a matrix that is the inverse of x
1225

1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
inv <- x$getinv()
28+
if(!is.null(inv)) {
29+
return(inv)
30+
}
31+
data <- x$get()
32+
inv <- solve(data, ...)
33+
x$setinv(inv)
34+
inv
1535
}

0 commit comments

Comments
 (0)