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
59 lines (45 loc) · 1.52 KB
/
cachematrix.R
File metadata and controls
59 lines (45 loc) · 1.52 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
## makeCacheMatrix creates a list containing a function to
## 1. set the value of the matrix
## 2. get the value of the matrix
## 3. set the value of inverse of the matrix
## 4. get the value of inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
## set the value of the matrix
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
## get the value of the matrix
get <- function() x
## set the inverse of the matrix
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
## get the inverse of the matrix
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
}
## Write a short comment describing this function
## cacheSolve returns the inverse of the matrix.
## First it checks if the inverse has already been computed.
## If yes, it gets that value.
## If no, it computes the inverse,
## and sets the value in the cache via setinverse function.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## get the inverse of the matrix
inv <- x$getinverse()
## check if the matrix exists
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
## get the inverse of the matrix
data <- x$get()
inv <- solve(data, ...)
## set the inverse of the matrix
x$setinverse(inv)
inv
}