Skip to content

Commit 669cabc

Browse files
committed
created cachematrix.r implementation
1 parent bb8f2e0 commit 669cabc

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

cachematrix.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ makeCacheMatrix <- function(x = matrix()) {
1515
m <<- NULL
1616
}
1717
get <- function() x
18-
setmatrix <- function(matrix) m <<- mean
19-
getmatrix <- function() m
18+
## Set and cache the inverse of the matrix using the solve function
19+
setinverse <- function(solve) m <<- inverse
20+
getinverse <- function() m
21+
## Create a list containing a function to set the value of the matrix, get the value of the matrix,
22+
## set the value of the inverse, and get the value of the inverse.
23+
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
2024

2125
}
2226

@@ -27,13 +31,16 @@ makeCacheMatrix <- function(x = matrix()) {
2731
## retrieve the inverse from the cache.
2832

2933
cacheSolve <- function(x, ...) {
30-
m <- x$getmatrix()
34+
m <- x$getinverse()
35+
## check to see if there is already a cached inverse
3136
if(!is.null(m)) {
3237
message("getting cached data")
3338
return(m)
3439
}
40+
41+
## If there is no cached inverse calculate the inverse, cache it and return it
3542
data <- x$get()
36-
m <- matrix(data, ...)
37-
x$setmatrix(m)
43+
m <- solve(data, ...)
44+
x$setinverse(m)
3845
m
3946
}

0 commit comments

Comments
 (0)