Skip to content

Commit 07b23e3

Browse files
DisturbedAddDisturbedAdd
DisturbedAdd
authored and
DisturbedAdd
committed
Solution for the rdpeng#3 week project
1 parent 7f657dd commit 07b23e3

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

cachematrix.R

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
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+
# This function creates a special "matrix" object that can cache its inverse.
52

63
makeCacheMatrix <- function(x = matrix()) {
4+
# Although the 'set' function is not required in the description of the task
5+
#there can be found a phrase in the description of cacheSolve function
6+
#"(and the matrix has not changed)" implying that the matrix can be changed.
77

8+
inv <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
inv <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(inv_2)
15+
inv <<- inv_2
16+
getinverse <- function()
17+
inv
18+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
819
}
920

10-
11-
## Write a short comment describing this function
21+
# This function computes the inverse of the special "matrix" returned by
22+
#makeCacheMatrix above.
23+
# If the inverse has already been calculated (and the matrix has not changed),
24+
#then the cachesolve should retrieve the inverse from the cache
1225

1326
cacheSolve <- function(x, ...) {
14-
## 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 <- solve(data, ...)
34+
x$setinverse(inv)
35+
inv
1536
}

0 commit comments

Comments
 (0)