forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
45 lines (32 loc) · 1012 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# HO.
# make a new cache matrix which is basically a list of four functions for:
# getting the matrix (get)
# setting the matrix (set)
# getting the inverse matrix (getinverse)
# setting the inverse matrix (setinverse)
makeCacheMatrix <- function( x = matrix() ) {
inverse <- NULL
set <- function( m ) {
x <<- m
inverse <<- NULL
}
get <- function() x
setinverse <- function( im ) {
inverse <<- im
}
getinverse <- function() inverse
list( set = set, get = get, setInverse = setinverse, getInverse = getinverse )
}
## expects a list returned by a call to makeCacheMatrix and if the inverse matrix is not already cached,
# calculates the inverse, caches and returns it
cacheSolve <- function( x, ... ) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getInverse()
if( !is.null( inverse ) ) {
message( "getting cached matrix" )
return( inverse )
}
inverse <- solve( x$get() )
x$setInverse( inverse )
inverse
}