Skip to content

Commit a54fefb

Browse files
committed
My answer to the second programming assignment version rdpeng#1
1 parent 7f657dd commit a54fefb

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cachematrix.R

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions combined, will be able to return an inverse of a matrix.
2+
## The function is written so that, should a matrix have remained the same, it will returned it's previously cached result, rather than computing the inverse again,
3+
## making the solution more efficient.
34

4-
## Write a short comment describing this function
5+
## makeCachematrix creates a 'special' matrix out of a simple matrix and makes it able to store the inverse in the cached memory
56

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

1019

11-
## Write a short comment describing this function
20+
## cacheSolve checks to see if the special matrix matches the special matrix from the previously cached inverse, and returns the cached inverse if true.
21+
## Should the special matrix have been altered, it computes and returns the appropriate inverse.
1222

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

0 commit comments

Comments
 (0)