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

Skip to content

Commit 65e1d4d

Browse files
author
Cristian Bidea
committed
Solving the assignment
1 parent 7f657dd commit 65e1d4d

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

cachematrix.R

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## makeCacheMatrix takes an matrix and creates an object from which you
2+
## can get the matrix back but also the inverse of the matrix if it has
3+
## been computed and then by calling the cacheSolve you can compute the
4+
## inverse of the matrix or take it from the cache if it was already
5+
## computed
36

4-
## Write a short comment describing this function
7+
## This function creates the special caching object
58

69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
inv <- NULL
11+
setData <- function (y)
12+
{
13+
x <<- y
14+
inv <<- NULL
15+
}
16+
getData <- function () x
17+
setInv <- function (invMat) inv <<- invMat
18+
getInv <- function () inv
19+
list (setData = setData, getData = getData, setInv = setInv, getInv = getInv)
820
}
921

1022

11-
## Write a short comment describing this function
23+
## This function will get you the inverse of a matrix but if that
24+
## was already computed then the computation is skipped and you get
25+
## the cached result
1226

1327
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
28+
inv <- x$getInv()
29+
if (!is.null(inv))
30+
{
31+
message("getting cached inverse")
32+
return(inv)
33+
}
34+
# if nothing in cache then compute
35+
data <- x$getData()
36+
inv <- solve(data)
37+
x$setInv(inv)
38+
x$getInv()
1539
}

0 commit comments

Comments
 (0)