Skip to content

Commit 342973b

Browse files
committed
implement functions and add comments
1 parent e821873 commit 342973b

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

cachematrix.R

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,39 @@
66
## same input matrix, the cached result will be returned, rather than the
77
## inverse being calculated again.
88

9+
## makeCacheMatrix: accepts a matrix as input, and returns
10+
## an object with functions for getting and setting the cached matrix, as
11+
## well as getting and calculating/setting the inverse matrix.
912

1013
makeCacheMatrix <- function(x = matrix()) {
11-
14+
m <- NULL
15+
set <- function(y) {
16+
x <<- y
17+
m <<- NULL
18+
}
19+
get <- function() x
20+
setinverse <- function(inverse) m <<- inverse
21+
getinverse <- function() m
22+
list(set = set, get = get,
23+
setinverse = setinverse,
24+
getinverse = getinverse)
1225
}
1326

1427

15-
## Write a short comment describing this function
28+
## cacheSolve: accepts an object that was created via makeCacheMatrix, and
29+
## if the inverse of the matrix has previously calculated, returns that
30+
## cached result. Otherwise, the inverse of the matrix is calcuated, stored
31+
## in the object passed in to cacheSolve, and then the inverse matrix is
32+
## returned.
1633

1734
cacheSolve <- function(x, ...) {
18-
## Return a matrix that is the inverse of 'x'
35+
m <- x$getinverse()
36+
if(!is.null(m)) {
37+
message("getting cached data")
38+
return(m)
39+
}
40+
data <- x$get()
41+
m <- solve(data, ...)
42+
x$setinverse(m)
43+
m
1944
}

0 commit comments

Comments
 (0)