-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
32 lines (29 loc) · 910 Bytes
/
cachematrix.R
File metadata and controls
32 lines (29 loc) · 910 Bytes
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
## Creates an object, contains a matrix data structure and its inverse
## get = gets the value of the matrix
## set = changes the value of the matrix
## getInverse = gets the cached inverse of the matrix once set
## setInverse = sets the cached inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
Im <- NULL
set <- function(y) {
x <<- y
Im <<- NULL
}
get <- function() x
setInverse <- function(solve) Im <<- solve
getInverse <- function() Im
list(set = set, get = get,setInverse = setInverse,getInverse = getInverse)
}
## returns the cached inverse value of cachematrix if available
## otherwise calculates, caches, and returns the inverse of the cachematrix
cacheSolve <- function(x=matrix(), ...) {
Im <- x$getInverse()
if(!is.null(Im)) {
message("getting cached data")
return(Im)
}
data <- x$get()
Im <- solve(data, ...)
x$setInverse(Im)
Im
}