forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
49 lines (36 loc) · 1.7 KB
/
cachematrix.R
File metadata and controls
49 lines (36 loc) · 1.7 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
#Creates a special matrix object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
inver <- NULL #initialise inverse variable and assign a NULL to it
# x comes from the parent environment. Here we assign it a value
set <- function(y = matrix()) {
x <<- y
inver <<- NULL #In case inver has already been set, it will be Null-ed here
}
get <- function() x
#Here I assign a value to the inver variable in the parent environment
setInver <- function(invers){
inver <<- invers
return(inver) #check what the inver value from the parent env is
}
#
getInver <- function() inver
list(set = set, get = get, setInver = setInver, getInver = getInver)
}
## Write a short comment describing this function
## Return a matrix that is the inverse of the special matrix returned by makeCacheMatrix above. check whether inversion has been done is included as well
cacheSolve <- function(x = makeCacheMatrix, ...) {
invrs <- x$getInver()
#In case a cache matrix is availale the code within the if is executed
if(!is.null(invrs)) {
message("Getting cached inversed matrix")
return(invrs)
}
#In case there is no cached matrix, it is going to be inverted
matrixSolveFunct <- x$get()
invrs <- solve(matrixSolveFunct, ...)
x$setinverse(invrs)
invrs #returns the inversed matrix
}