Skip to content

Commit 53e5aa6

Browse files
committed
Added function body for makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit 53e5aa6

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

cachematrix.R

Lines changed: 26 additions & 5 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
3-
4-
## Write a short comment describing this function
1+
## The following function creates a special "matrix" object
2+
## that can cache its inverse.
53

64
makeCacheMatrix <- function(x = matrix()) {
5+
m <- NULL
6+
set <- function(y) {
7+
x <<- y
8+
m <<- NULL
9+
}
10+
get <- function() x
11+
setsolve <- function(solve) m <<- solve
12+
getsolve <- function() m
13+
list(set = set, get = get,
14+
setsolve = setsolve,
15+
getsolve = getsolve)
716

817
}
918

1019

11-
## Write a short comment describing this function
20+
## This function computes the inverse of the special
21+
## "matrix" returned by `makeCacheMatrix` above. If the inverse has
22+
## already been calculated (and the matrix has not changed), then
23+
## `cacheSolve` should retrieve the inverse from the cache.
1224

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

0 commit comments

Comments
 (0)