Skip to content

Commit 57c5123

Browse files
committed
Update cachematrix.R
1 parent 7f657dd commit 57c5123

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

cachematrix.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These function are used to make a matrix object that can cache its inverse
2+
## and return the cached inverse of that matrix
33

4-
## Write a short comment describing this function
4+
## makeCacheMatrix makes a special matrix object that can cache its inverse
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
m <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setInv <- function(solve) m <<- solve
14+
getInv <- function() m
15+
list(set = set, get = get,
16+
setInv = setInv,
17+
getInv = getInv)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## cacheSolve then computes the inverse of the special matrix returnet by the
22+
## function makeCacheMatrix.
23+
## If the inverse has alredy been calculated and the matrix is the same
24+
## then cacheSolve should retrieve the inverse from the cache
1225

1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
m <- x$getInv()
28+
if(!is.null(m)) {
29+
message("getting cached data")
30+
return(m)
31+
}
32+
data <- x$get()
33+
m <- solve(data, ...)
34+
x$setInv(m)
35+
m
1536
}

0 commit comments

Comments
 (0)