Skip to content

Commit b91caec

Browse files
committed
Initial commit
1 parent 7f657dd commit b91caec

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

cachematrix.R

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Functions for computing matrix inverses and saving a cache
2+
## to remove the need for recomputation
33

4-
## Write a short comment describing this function
4+
## makeCacheMatrix returns an object with methods for getting/setting
5+
## a matrix and its inverse
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
matr <- x
9+
inv <- NULL
10+
get <- function() matr
11+
set <- function(newMatr) {
12+
matr <<- newMatr
13+
inv <<- NULL
14+
}
15+
getinv <- function() inv
16+
setinv <- function(newInv) {
17+
inv <<- newInv
18+
}
19+
list(set = set, setinv = setinv,
20+
get = get, getinv = getinv)
821
}
922

1023

11-
## Write a short comment describing this function
24+
## Computes the inverse of a matrix or returns a cached value if
25+
## one is present
1226

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

0 commit comments

Comments
 (0)