Skip to content

Commit c98de05

Browse files
committed
Update cachematrix.R
1 parent 7f657dd commit c98de05

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

cachematrix.R

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,66 @@ makeCacheMatrix <- function(x = matrix()) {
1313
cacheSolve <- function(x, ...) {
1414
## Return a matrix that is the inverse of 'x'
1515
}
16+
17+
18+
19+
## A pair of functions that inverse matrices with caching
20+
21+
makeCacheMatrix <- function(m = matrix()) {
22+
# Initialize the inverse property
23+
i <- NULL
24+
25+
26+
# Set the matrix
27+
set <- function(mat) {
28+
m <<- mat
29+
i <<- NULL
30+
}
31+
32+
# get the matrix
33+
34+
get <- function() {
35+
# return the matrix
36+
m
37+
}
38+
# the inverse matrix
39+
40+
setinverse <- function(inverse){
41+
i <<- inverse
42+
}
43+
44+
# get the inverse matrix
45+
46+
getinverse <- function(){
47+
i
48+
}
49+
# return list of methods
50+
list(set = set,
51+
get = get,
52+
setinverse = setinverse,
53+
getinverse = getinverse)
54+
}
55+
56+
57+
# Part two
58+
59+
cacheSolve <- function(x, ...) {
60+
# return the inverse of x
61+
m <- x$getinverse()
62+
63+
if(!is.null(m)) {
64+
65+
message("getting cached data.")
66+
67+
return(m)
68+
}
69+
70+
data <- x$get()
71+
# the inverse via matrix multiplication
72+
m <- solve(data) %*% data
73+
74+
x$setinverse(m)
75+
76+
# return the matrix
77+
m
78+
}

0 commit comments

Comments
 (0)