Skip to content

Commit 3415a75

Browse files
committed
Created functions makeCacheMatrix and cacheSolve.
1 parent 9b22d21 commit 3415a75

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

cachematrix.R

Lines changed: 36 additions & 15 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
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
1+
## The functions below create a special vector that stores a matrix along with
2+
## its cached inverse if the inverse has been previously solved by cacheSolve
3+
## below
4+
5+
## makeCacheMatrix creates a special vector that stores a matrix and its
6+
## cached inverse if computed.
7+
8+
makeCacheMatrix <- function(x = matrix()) {
9+
m <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
get <- function() x
15+
setmatrix <- function(matrix) m <<- matrix
16+
getmatrix <- function() m
17+
list(set = set, get = get,
18+
setmatrix = setmatrix,
19+
getmatrix = getmatrix)
20+
}
21+
22+
## cacheSolve checks to see if the inverse of a matrix is stored in the
23+
## vector created by makeCacheMatrix, if so it returns the stored value
24+
## if not it computes the inverse and stores the value in the cache.
25+
26+
cacheSolve <- function(x, ...) {
27+
m <- x$getmatrix()
28+
if(!is.null(m)) {
29+
message("getting cached data")
30+
return(m)
31+
}
32+
data <- x$get()
33+
m <- solve(data, ...)
34+
x$setmatrix(m)
35+
m
36+
}

0 commit comments

Comments
 (0)