Skip to content

Commit 49d52d7

Browse files
author
Paulo Eduardo Deininger Messias Alves
committed
Programming Assignment 2: Lexical Scoping
1 parent 7f657dd commit 49d52d7

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

cachematrix.R

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Caching the Inverse of a Matrix
32

4-
## Write a short comment describing this function
3+
## This function creates a special "matrix" object that can cache its inverse
54

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

1019

11-
## Write a short comment describing this function
20+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above
1221

1322
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
23+
m <- x$getsolve()
24+
if(!is.null(m)) {
25+
message("getting cached data")
26+
return(m)
27+
}
28+
data <- x$get()
29+
m <- solve(data, ...)
30+
x$setsolve(m)
31+
m
1532
}

0 commit comments

Comments
 (0)