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
40 lines (34 loc) · 1.24 KB
/
cachematrix.R
File metadata and controls
40 lines (34 loc) · 1.24 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
## makeCacheMatrix creates an object that allows us to store a matrix and
## retrieve it. It also allows us to get and set its inverse
makeCacheMatrix <- function(x = matrix()) {
## Set the inverse to null on construction
m <- NULL
## Again set the inverse to null if we assign a new matrix to the object
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinverse <- function(solve) m <<- solve
getinverse <- function() m
## Supply a list of pointers to our functions
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## The cashSolve function returns the inverse of the matrix in the
## makeCacheMatrix object. Otherwise it calculates and stores it in the
## makeCacheMatrix before returning it
cacheSolve <- function(x, ...) {
m <- x$getinverse()
## If the inverse is already cached then return it
if(!is.null(m)) {
message("getting cached inverse")
return(m)
}
## Otherwise, we calculate the inverse
data <- x$get()
m <- solve(data)
x$setinverse(m)
m
}