Skip to content

Commit 797dc86

Browse files
committed
Copy the code for mean and rewrite for matirx
1 parent e03c611 commit 797dc86

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

cachematrix.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@
55
## matrix inversion calculations.
66

77
makeCacheMatrix <- function(x = matrix()) {
8-
8+
inv <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
inv <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(solve) inv <<- solve
15+
getinverse <- function() solve
16+
list(set = set,
17+
get = get,
18+
setinverse = setinverse,
19+
getinverse = getinverse)
920
}
1021

1122

1223
## This function calculates the inverse.
1324

1425
cacheSolve <- function(x, ...) {
15-
## Return a matrix that is the inverse of 'x'
26+
## Return a matrix that is the inverse of 'x'
27+
inv <- x$getinverse()
28+
if(!is.null(inv)) {
29+
message("getting cached data")
30+
return(inv)
31+
}
32+
data <- x$get()
33+
inv <- mean(data, ...)
34+
x$setinverse(inv)
35+
inv
1636
}

0 commit comments

Comments
 (0)