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
38 lines (29 loc) · 1.26 KB
/
cachematrix.R
File metadata and controls
38 lines (29 loc) · 1.26 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
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
makeCacheMatrix <- function (x = matrix()) {
cache <- NULL # Default the value of cached value to NULL
set <- function (y) {
x <<- y
cache <<- NULL
} # create the matrix in working environment
get <- function() x # get the value of the matrix
setInverse <- function (inverse) # set the inverted matrix
cache<<- inverse
getInverse <- function() cache # get the inverted matrix
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
cacheSolve <- function(x, ...) {
# returned a matrix that is the inverse of x
cache <- x$getInverse() # return inverted matrix if exists
if(!is.null(cache)) {
message("getting cached data") # print comment and skip the calcuation
return(cache)
}
matrix <- x$get() # create matrix if it does not exist
cache <- solve(matrix, ...) # return the inverted matrix
x$setInverse(cache) # set the value of matrix in cache
cache # return cached value of matrix
}