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
49 lines (38 loc) · 1.25 KB
/
cachematrix.R
File metadata and controls
49 lines (38 loc) · 1.25 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
## Mentor Graded Assignment: Programming Assignment 2: Lexical Scoping
## Course: R Programming
## Efficent caching of inverse of matrices, reuses pre-computed values whenever possible
## Create an enhancd matrix object, which is capable of keeping it inverse value
## if already computed
makeCacheMatrix <- function(x = matrix()) {
##CAched inverse of the matrix
cachedInverse <- NULL
set <- function(value) {
x <<- value
cachedInverse <<- NULL
}
get <- function() x
setInverse <- function(inverse) {
cachedInverse <<- inverse
}
getInverse <- function() cachedInverse
##Return enhanced Matrix object
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Computes the inverse of a matrix. Whenever a previous solution is already available
## (cahced) for a given Matrix, the cached value is returned
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
cachedValue <- x$getInverse()
if (is.null(cachedValue)) {
##Compute inverse of matrix
inverse <- solve(x$get())
##Store result in cache
x$setInverse(inverse)
##Return cached result
inverse
} else {
## Return cached value
message("Cached value found!")
cachedValue
}
}