Skip to content

Commit 48016c8

Browse files
committed
Programming assignemnt exercise. Create two functions around matrix inversion and caching.
1 parent 7f657dd commit 48016c8

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

cachematrix.R

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions have been developed as part of a Coursera R Programming course
2+
## assignment.
33

4-
## Write a short comment describing this function
4+
## Creates a set of utility functions to work with a matrix and cache the results of
5+
## inverting it. It's assumed the matrix is always invertible.
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
s <- NULL
9+
10+
set <- function(y) {
11+
x <<- y
12+
s <<- NULL
13+
}
14+
15+
get <- function() x
16+
17+
setsolve <- function(solve) s <<- solve
18+
19+
getsolve <- function() s
20+
21+
list(set = set, get = get,
22+
setsolve = setsolve,
23+
getsolve = getsolve)
824
}
925

1026

11-
## Write a short comment describing this function
27+
## Inverts a matrix, but caches the result for subsequent invocations.
28+
## x should be a list of functions as returned by makeCacheMatrix
1229

1330
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
31+
s <- x$getsolve()
32+
33+
if(!is.null(s)) {
34+
message("getting cached data")
35+
return(s)
36+
}
37+
38+
data <- x$get()
39+
s <- solve(data, ...)
40+
x$setsolve(s)
41+
42+
s
1543
}

0 commit comments

Comments
 (0)