forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
33 lines (32 loc) · 1.01 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
30
31
32
33
##Creates a special matix which contains its corresponding inverse
##Creates the getter and setter methods for the acutal matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
##Setter and getter for actual matrix
setInverse <- function(inverse) m <<- inverse
getInverse <- function() m
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
##Setter and getter for inverse matrix
}
##Solves the inverse of matrix using solve function
##Before solving it checks if the input has inverse precomputed
##If it is it just returns the precomputed inverse
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
} ##returns via cache
data <- x$get()
m <- solve(data) ##computes the inverse
x$setInverse(m) ##Sets the inverse back to the list
m ##prints the result
}