Skip to content

Commit bfb55e5

Browse files
1 parent 7f657dd commit bfb55e5

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

cachematrix.R

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,61 @@
55

66
makeCacheMatrix <- function(x = matrix()) {
77

8+
## Initialize the inverse property
9+
i <- NULL
10+
11+
## Method to set the matrix
12+
set <- function( matrix ) {
13+
m <<- matrix
14+
i <<- NULL
15+
}
16+
17+
## Method the get the matrix
18+
get <- function() {
19+
## Return the matrix
20+
m
21+
}
22+
23+
## Method to set the inverse of the matrix
24+
setInverse <- function(inverse) {
25+
i <<- inverse
26+
}
27+
28+
## Method to get the inverse of the matrix
29+
getInverse <- function() {
30+
## Return the inverse property
31+
i
32+
}
33+
34+
## Return a list of the methods
35+
list(set = set, get = get,
36+
setInverse = setInverse,
37+
getInverse = getInverse)
38+
839
}
940

1041

1142
## Write a short comment describing this function
1243

1344
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
45+
46+
m <- x$getInverse()
47+
48+
49+
if( !is.null(m) ) {
50+
message("getting cached data")
51+
return(m)
52+
}
53+
54+
## Get the matrix from our object
55+
data <- x$get()
56+
57+
## Calculate the inverse using matrix multiplication
58+
m <- solve(data) %*% data
59+
60+
## Set the inverse to the object
61+
x$setInverse(m)
62+
63+
## Return the matrix
64+
m
1565
}

0 commit comments

Comments
 (0)