Skip to content

Commit dba0be7

Browse files
committed
expands the makeCacheMatrixand cacheSolve functions
1 parent 7f657dd commit dba0be7

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

cachematrix.R

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,46 @@
22
## functions do
33

44
## Write a short comment describing this function
5+
###The first function, makeCacheMatrix creates a special "matrix"
6+
###which is really a list containing a function to
7+
###*set the value of the matrix
8+
###*get the value of the matrix
9+
###*set the value of the inverse
10+
###*get the value of the inverse
511

612
makeCacheMatrix <- function(x = matrix()) {
7-
13+
m <- NULL
14+
set <- function(y) {
15+
x <<- y
16+
m <<- NULL
17+
}
18+
get <- function() x
19+
setinverse <- function(solve) m <<- solve
20+
getinverse <- function() m
21+
list(set = set, get = get,
22+
setinverse = setinverse,
23+
getinverse = getinverse)
824
}
925

1026

1127
## Write a short comment describing this function
28+
###The following function calculates the inverse of the
29+
###special "matrix" created with the above function.
30+
###However, it first checks to see if the inverse has
31+
###already been calculated. If so, it gets the inverse
32+
###from the cache and skips the computation.
33+
###Otherwise, it calculates the mean of the data and sets
34+
###the value of the inverse in the cache via the setinverse function.
1235

1336
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
37+
## Return a matrix that is the inverse of 'x'
38+
m <- x$getinverse()
39+
if(!is.null(m)) {
40+
message("getting cached data")
41+
return(m)
42+
}
43+
data <- x$get()
44+
m <- solve(data, ...)
45+
x$setinverse(m)
46+
m
47+
}

0 commit comments

Comments
 (0)