forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoursera
30 lines (28 loc) · 827 Bytes
/
Coursera
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
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
# set the value of the matrix
set <- function(y) {
x <<- y
inv <<- NULL
}
# get the value of the matrix
get <- function() x
# set the value of the inverse
setinverse <- function(inverse) inv <<- inverse
# get the value of the inverse
getinverse <- function() inv
# return:
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
cacheSolve <- function(x, ...) {
inv <- x$getinverse() # function() inv
if(!is.null(inv)) {
message("getting cached data")
return(inv) # return ends the function
}
data <- x$get() # function() x
inv <- solve(data, ...) # calculates the inverse and stores it as inv
x$setinverse(inv) # function(inverse) inv <<- inverse
inv
}