forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
37 lines (32 loc) · 994 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
## The functions below calculate the inverse of an input matrix which
## uses the cache method to avoid repetitive computation of the matrix
## inverse.
## This function defines a set of four functions to help to cache the
## matrix inverse. It returns a list of functions.
makeCacheMatrix <- function(m = matrix()) {
M<-NULL
set<-function(y)
{
m<<-y
M<<-NULL
}
get<-function() m
setinverse<-function(inv) M<<-inv
getinverse<-function() M
list(set=set, get=get, setinverse=setinverse,
getinverse=getinverse)
}
## This function computes the inverse of the matrix returned by makeCacheMatrix
## above. The cache will be checked first in order to avoid repetitive calculation.
cacheSolve <- function(x, ...) {
M<-x$getinverse()
if(!is.null(M)) {
message("getting cached data")
return(M)
}
data <- x$get()
M <- solve(data, ...)
x$setinverse(M)
M
## Return a matrix that is the inverse of 'x'
}