Skip to content

Commit 92d3d2f

Browse files
committed
Initial Answer
1 parent 7f657dd commit 92d3d2f

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

cachematrix.R

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## cachematrix.R
2+
## This file contains a pair of functions that cache the inverse of a matrix when used together.
33

4-
## Write a short comment describing this function
4+
## makeCacheMatrix: This function takes a matrix and returns a list object (l) containing the following functions:
5+
## l$setmatrix - caches the matrix to be inverted
6+
## l$getmatrix - returns the matrix to be inverted
7+
## l$setinvmatrix - caches the inverted matrix
8+
## l$getinvmatrix - returns the inverted matrix
59

610
makeCacheMatrix <- function(x = matrix()) {
7-
11+
m <- NULL
12+
setmatrix <- function(y) {
13+
x <<- y
14+
m <<- NULL
15+
}
16+
getmatrix <- function() x
17+
setinvmatrix <- function(inverse) m <<- inverse
18+
getinvmatrix <- function() m
19+
list(setmatrix = setmatrix, getmatrix = getmatrix,
20+
setinvmatrix = setinvmatrix,
21+
getinvmatrix = getinvmatrix)
822
}
923

1024

11-
## Write a short comment describing this function
25+
## cacheSolve: This function takes a list object (x) returned by the makeCacheMatrix function above and either
26+
## returns the cached inverted matrix stored in x, or calculates the inverse of the matrix cached in x and
27+
## stores it in x.
1228

1329
cacheSolve <- function(x, ...) {
1430
## Return a matrix that is the inverse of 'x'
31+
m <- x$getinvmatrix()
32+
if (!is.null(m)) {
33+
return(m)
34+
}
35+
mat <- x$getmatrix()
36+
m <- solve(mat, ...)
37+
x$setinvmatrix(m)
38+
m
1539
}

0 commit comments

Comments
 (0)