forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
45 lines (32 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## Creates a matrix that caches its inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
#Set the value of the matrix
set <- function(y){
x <<- y
m <<- NULL
}
#Get the value of the matrix
get <- function() x
#Set the value of the inverse matrix using the Solve function
inversematrix <- function(solve) m<<- solve
#Get the value of the inverse matrix
getmatrix <- function() m
list(set=set, get=get, inversematrix=inversematrix, getmatrix=getmatrix)
}
## Computes the inverse of the matrix returned by makeCacheMatrix. If the inverse has already been calculated
## then the cachesolve should retrieve the inverse from the cache.
cacheSolve <- function(x=matrix(), ...) {
m<-x$getmatrix()
# Returns cached matrix if already calculated
if(!is.null(m)){
message("Getting Cached Inverse Matrix")
return(m)
}
#Computes the inveerse of the matrix if not already cached
matrix<-x$get()
m<-solve(matrix, ...)
x$inversematrix(m)
## Return a matrix that is the inverse of 'x'
m
}