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
52 lines (47 loc) · 1.53 KB
/
cachematrix.R
File metadata and controls
52 lines (47 loc) · 1.53 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
## to cache up internal state of a matrix in order to save time taken
## on recomputation
## to create a special 'matrix' which is able to cache inverse matrix
makeCacheMatrix <- function( x = matrix() ) {
## inverse matrix cache, initially set to NULL
im <- NULL
## set the value of the matrix
set <- function( y ) {
x <<- y
im <<- NULL
}
## get the vlaue of the matrix
get <- function() {
x
}
## set the value of the inverse matrix
setInverse <- function( m ) {
im <<- m
}
## set the value of the inverse matrix
getInverse <- function() {
im
}
list( set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse )
}
## to calculate the inverse matrix of the special 'matrix' created
## with makeCacheMatrix
cacheSolve <- function( x, ... ) {
## retrieve internally cached inverse matrix
im <- x$getInverse()
## if the cached inverse matrix is not null, use it as the
## result of this function
if( ! is.null( im ) ) {
message( "getting cached data" )
return( im )
}
## otherwise, compute the inverse matirx in the original way
data <- x$get()
im <- solve( data, ... )
## store the inverse matrix in the cache
x$setInverse( im )
## return the inverse matrix
im
}