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
34 lines (29 loc) · 1.13 KB
/
Copy pathcachematrix.R
File metadata and controls
34 lines (29 loc) · 1.13 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
## Functions to demonstrate caching of the inverse of a matrix.
## Below function makes a special "matrix" which is actually a list containing functions to get/set the matrix and get/set the inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y) {
x <<- y
inverse <<- NULL
}
get <- function() x
setinv <- function(solve) inverse <<- solve
getinv <- function() inverse
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## The below function calculates the inverse of the matrix created using the function above, it first checks if the inverse has been already created. If yes it
## skips calculating the inverse otherwise calculated it.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getinv()
if(!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
data <- x$get()
inverse <- solve(data, ...)
x$setinv(inverse)
inverse
}