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

Skip to content

Commit ee75f9e

Browse files
committed
My solution.
1 parent 7f657dd commit ee75f9e

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

cachematrix.R

Lines changed: 25 additions & 5 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
1+
## Define a cached matrix class that can cache
2+
## the inverse.
33

4-
## Write a short comment describing this function
4+
## Construct a cached matrix from a matrix
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inv <- NULL
8+
set <- function(y){
9+
x <<- y
10+
inv <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) inv <<- inverse
14+
getinverse <- function() inv
15+
list( set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## Compute inverse of a cached matrix. If already
22+
## computed, cached version is used.
1223

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

0 commit comments

Comments
 (0)