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

Skip to content

Commit 33de527

Browse files
committed
1st version of solution of programming assignment
1 parent 7f657dd commit 33de527

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

cachematrix.R

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
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+
## A set of functions to create a matrix with a cached value of the inverse
2+
## Use the function cacheSolve to use the cached value if it exists
53

4+
## Make a matrix with a function to cache the inverse value
65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
cached_inverse <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
cached_inverse <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(inverse) cached_inverse <<- inverse
13+
getinverse <- function() cached_inverse
14+
list(set = set, get = get,
15+
setinverse = setinverse,
16+
getinverse = getinverse)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## Returns a matrix that is the inverse of 'x',
21+
## using the cached value in x if available
22+
## Note: x should be an object created with makeCacheMatrix, it can not be an ordinary matrix
1223

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

0 commit comments

Comments
 (0)