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
56 lines (46 loc) · 1.86 KB
/
Copy pathcachematrix.R
File metadata and controls
56 lines (46 loc) · 1.86 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
53
54
55
56
## This function provides basic caching operations,
## which include initializing, setting matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
i <- NULL #initialize the matrix inverse
set <- function(y) {
## we set our matrix x with the value of y
## note <<- operator, also we set the inverse to NULL
x <<- y
i <<- NULL
}
get <- function() x # we return the matrix x
setinverse <- function(inverse) i <<- inverse #we set the matrix inverse in this function
getinverse <- function() i #get the matrix inverse in this function
## return a list that contains the four functionalities
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## CacheSolve will try to inverse a matrix, first it will check if the
## inverse is available, then no need to calculate. Otherwise, it will
## calculate the inverse and cache it for further computation if needed
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## x here is the object returned from makeCacheMatrix function
i <- x$getinverse()
if(!is.null(i)) {
message("getting cached matrix")
return(i)
}
## No need for else as we return if it is cached
## In the following 3 steps we get the matrix,
## calculate the index, and cache it back into the matrix
myMatrix <- x$get()
inverse = solve(myMatrix)
x$setinverse(inverse)
#return the inverse
inverse
}
## Example:
## a = matrix(1:4,2,2)
## x <- makeCacheMatrix()
## x$set(a)
## b<-cacheSolve(x)
##validate
## a%*%b
## This should give identity matrix