forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
42 lines (37 loc) · 1.36 KB
/
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
## The <<- operator can be used to assign a value to an object in an environment that is different from the current environment.
## The functions below are used to create a special object that stores a matrix and caches its inverse.
## Creating a cache saves time so the inverse (of the same matrix) does not need to be calculated many times in the program.
##
##
## This function creates a list containing functions to set the matrix,
## get the matrix, set the inverse and get the inverse. It creates a special "matrix" object.
##
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y){
x <<- y
m <<- NULL
}
get <- function() x
setinverse <-function(solve) m <<-solve
getinverse <- function()m
list(set=set,get=get,
setinverse=setinverse,
getinverse=getinverse)
}
## This function calculates the inverse of the special "matrix" object returned from the makeCacheMatrix function.
## It first checks to see if the inverse has already been calculated and cached, if so
## it uses the cached inverse, thereby skipping the expensive computation. If not, it calculates the inverse,
## and then caches it.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getinverse()
if(!is.null(m)){
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data,...)
x$setinverse(m)
m
}