Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 2264a23

Browse files
author
Dmitriy Morozov
committed
Solution for the 'Caching the Inverse of a Matrix' assignment
1 parent 7f657dd commit 2264a23

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

cachematrix.R

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Following is a pair of functions that cache the inverse of a matrix.
52

3+
## Creates a special "matrix" object that can cache it's inverse
64
makeCacheMatrix <- function(x = matrix()) {
7-
5+
m <- NULL
6+
7+
set <- function(y) {
8+
x <<- y
9+
m <<- NULL
10+
}
11+
get <- function() x
12+
setInverse <- function(matrix) m <<- matrix
13+
getInverse <- function() m
14+
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
815
}
916

10-
11-
## Write a short comment describing this function
12-
17+
## Computes the inverse of the special "matrix" returned by makeCacheMatrix function above.
18+
## If the inverse has already been calculated, then returns the inverse from the cache.
1319
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
20+
m <- x$getInverse()
21+
if(!is.null(m)) {
22+
message("getting cached data")
23+
return(m)
24+
}
25+
data <- x$get()
26+
m <- solve(data, ...)
27+
x$setInverse(m)
28+
m
1529
}

0 commit comments

Comments
 (0)