forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
31 lines (29 loc) · 825 Bytes
/
cachematrix.R
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
##makeCacheMatrix : makes special matrix that can cache inverse matrix
##To be easily understood, "im" is used for an attribute.
## make special matrix.
makeCacheMatrix <- function(x = matrix()){
im <- NULL
set <- function(y){
x<<-y
im<<-NULL
}
get <- function() x
setIMatrix <- function(mat) im<<-mat
getIMatrix <- function() im
list(set = set,get = get,
setIMatrix = setIMatrix,
getIMatrix = getIMatrix)
}
##caculate inverse matrix as necessary, and caches it.
##when there is already cache, print message that the program is using the cache.
cacheSolve <- function(x, ...){
im<-x$getIMatrix()
if(!is.null(im)){
message("getting cache data")
return(im)
}
data <- x$get()
im <- solve(data, ...)
x$setIMatrix(im)
im
}