forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
35 lines (30 loc) · 951 Bytes
/
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
## The functions makeCacheMatrix and cacheSolve allow you to cache the inverse of a matrix
## then result will be evaluated if same inverse is needed for other calculations
## makeCacheMatrix to create a special object as matrix will be stored in the cache
makeCacheMatrix <- function(x = matrix()) {
j <- NULL
set <- function(y){
x <<- y
j <<- NULL
}
get <- function()x
setInverse <- function(solve)j <<- solve
getInverse <- function()j
list (set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## cacheSolve to compute inverse of matrix or retrieve the inverse if it has
## already been calculated and matrix has not changed
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
j <- x$getInverse()
if(!is.null(j)) {
message("getting cached data")
return(j)
}
matrix_inv <- x$get()
j <- solve(matrix_inv,...)
x$setInverse(j)
j
}