File tree Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 1+ makeVector <- function (x = numeric ()) {
2+ m <- NULL
3+ set <- function (y ) {
4+ x <<- y
5+ m <<- NULL
6+ }
7+ get <- function () x
8+ setmean <- function (mean ) m <<- mean
9+ getmean <- function () m
10+ list (set = set , get = get ,
11+ setmean = setmean ,
12+ getmean = getmean )
13+ }
14+
15+
16+ cachemean <- function (x , ... ) {
17+ m <- x $ getmean()
18+ if (! is.null(m )) {
19+ message(" getting cached data" )
20+ return (m )
21+ }
22+ data <- x $ get()
23+ m <- mean(data , ... )
24+ x $ setmean(m )
25+ m
26+ }
Original file line number Diff line number Diff line change 44# # Write a short comment describing this function
55
66makeCacheMatrix <- function (x = matrix ()) {
7-
7+ cachedmatrix <- NULL
8+ set <- function (y ) {
9+ x <<- y
10+ cachedmatrix <<- NULL
11+ }
12+ get <- function () {
13+ x
14+ }
15+ setinverse <- function (solve ) {
16+ cachedmatrix <<- solve
17+ }
18+ getinverse <- function () {
19+ cachedmatrix
20+ }
21+ list (set = set , get = get ,
22+ setinverse = setinverse ,
23+ getinverse = getinverse )
824}
925
1026
1127# # Write a short comment describing this function
1228
1329cacheSolve <- function (x , ... ) {
1430 # # Return a matrix that is the inverse of 'x'
31+
32+ cachedmatrix <- x $ getinverse()
33+ if (! is.null(cachedmatrix )) {
34+ message(" getting cached data" )
35+ return (cachedmatrix )
36+ }
37+ data <- x $ get()
38+ cachedmatrix <- solve(data )
39+ x $ setinverse(cachedmatrix )
40+ cachedmatrix
1541}
You can’t perform that action at this time.
0 commit comments