Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9416291

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

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

cachematrix.R

Lines changed: 27 additions & 7 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
1+
## Use example:
2+
## a <- makeCacheMatrix(matrix(1:4,2,2))
3+
## cacheSolve(a) # real computations
4+
## cacheSolve(a) # extracting from cache
55

6+
## Make a special cache object, vary much like a class.
7+
## Same as original assignment
68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
m <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
get <- function() x
15+
setinv <- function(inv) m <<- inv
16+
getinv <- function() m
17+
list(set = set, get = get,
18+
setinv = setinv,
19+
getinv = getinv)
820
}
921

1022

11-
## Write a short comment describing this function
23+
## Same function as original, just use solve instead mean
1224

1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
m <- x$getinv()
27+
if(!is.null(m)) {
28+
message("getting cached data")
29+
return(m)
30+
}
31+
data <- x$get()
32+
m <- solve(data, ...)
33+
x$setinv(m)
34+
m
1535
}

0 commit comments

Comments
 (0)