Skip to content

Commit 79adc39

Browse files
committed
Completed and tested the 2 functions
Completed and tested the 2 functions. Used system.time to compare speed of solve vs cacheSolve on a 10x10 matrix. cacheSolve was (only) 12 times faster¬!
1 parent 7f657dd commit 79adc39

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

cachematrix.R

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
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+
## Put comments here that give an overall description of what your
2+
## functions do
3+
4+
## This creates a new object that stores a matrix
5+
## together with it's inverse - initially set to NULL
6+
7+
makeCacheMatrix <- function(x = matrix()) {
8+
mi <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
mi <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(inv) mi <<- inv
15+
getinverse <- function() mi
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
19+
}
20+
21+
22+
## Write a short comment describing this function
23+
24+
cacheSolve <- function(x, ...) {
25+
## Return a matrix that is the inverse of 'x'
26+
mi <- x$getinverse()
27+
if(!is.null(mi)) {
28+
#message("getting cached matrix inverse")
29+
return(mi)
30+
}
31+
data <- x$get()
32+
mi <- solve(data, ...)
33+
x$setinverse(mi)
34+
mi
35+
}

0 commit comments

Comments
 (0)