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
55 lines (42 loc) · 1.44 KB
/
cachematrix.R
File metadata and controls
55 lines (42 loc) · 1.44 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
# Returns the inverse of a matrix using cacheSolve
# The cacheSolve will also store a copy of the cached matrix
# so this does not need to be recomputed for future calls.
# example usage:
#> m <- matrix(c(2, 3, 2, 2), nrow=2, ncol=2)
#> cacheMatrix <- makeCacheMatrix(m)
#> cacheSolve(cacheMatrix)
## Return a list of functions to operate on a special Matrix
## set,get for updating the matrix
## setinverse,getinverse for storing the cached inverse
makeCacheMatrix <- function(x = matrix()) {
cm <- NULL
# Sets the matrix and clears the cache
set <- function(y) {
x <<- y
cm <<- NULL
}
# Returns the matrix
get <- function() { x }
# Sets the cached inverse of the matrix
setinverse <- function(inverse) { cm <<- inverse }
# Returns the cached inverse of the matrix
# This is null unless setinverse has been called
getinverse <- function() { cm }
list(set = set, get = get,
setinverse = setinverse, getinverse = getinverse)
}
## Return a matrix that is the inverse of cacheMatrix 'x'
## If x has already been solved then we use the cached copy
cacheSolve <- function(x, ...) {
# Checks and returns cached copy if one exists
cm <- x$getinverse()
if(!is.null(cm)) {
message("Using cached copy.")
return(cm)
}
# Solves and caches the matrix
data <- x$get()
cm <- solve(data)
x$setinverse(cm)
cm
}