Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
SourinRakshit committed Jul 14, 2021
1 parent 7f657dd commit 9ac2322
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cachematrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@
## Write a short comment describing this function

makeCacheMatrix <- function(x = matrix()) {

inv <- NULL ## initialize inv as NULL; will hold value of inverse matrix
set <- function(y) { ## set to assign new function
x <<- y ## value of matrix in parent environment
inv <<- NULL ## if there is a new matrix, reset inv to NULL
}
get <- function() {x} ## define the get function - returns value of the matrix argument
setInverse <- function(inverse) {inv <<- inverse} ## assigns value of inv in parent environment
getInverse <- function() {inv} ## gets the value of inv where called
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}


## Write a short comment describing this function

cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getInverse()
if(!is.null(inv)) { ## if inverse has not been already cached, it is cached and collected
message("cached data collected")
return(inv)
}
m <- x$get()
inv <- solve(m, ...)
x$setInverse(inv)
inv
}

0 comments on commit 9ac2322

Please sign in to comment.