forked from catalinp99/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
29 lines (26 loc) · 1.04 KB
/
cachematrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## This is my submission on lexical scoping for week 3 of R Programming on Coursera
## The fundamental function here is makeCacheMatrix
## This function has set, get, setInv and getInv
## All of these functions are made use of in the following code
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL ##This will initialize the inverse as NULL
set <- function(y){
x <<- y
inv <<- NULL
}
get <- function() {x} ##Then the function gets the matrix x
setinverse <- function(inverse) {inv <<- inverse}
getinverse <- function() {inv}
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
}
cacheSolve <- function(x, ...){
inv <- x$getinverse()
if(!is.null(inv)){ ##Checking if the inverse process would be NULL
message("getting the cached data")
return(inv) ##Then returns if so
}
mat <- x$get()
inv <- solve(mat, ...)
x$setinverse(inv)
inv ##Finally, a matrix returned by which x is of inverse
}