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
39 lines (30 loc) · 1.01 KB
/
Copy pathcachematrix.R
File metadata and controls
39 lines (30 loc) · 1.01 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
# The function takes a matrix and defines up 3 functions
# to work on it in the following cacheSolve(). They are returned in a list.
# It works by using:
# matrix <- #our matrix#
# a <- makeCacheMatrix(matrix)
# cacheSolve(a)
# The function is adapted from the example.
# It assumes that the matrix can be inversed.
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
get <- function() {x}
setInverse <- function(inverse) i <<- inverse
getInverse <- function() {i}
list(get = get, setInverse = setInverse, getInverse = getInverse)
}
# Takes the previous function, checks if i has already been computed and
# if not, computes the inversion. In both cases, the inverted matrix is returned.
cacheSolve <- function(x, ...) {
i <- x$getInverse()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
data <- x$get()
i <- solve(data, ...)
x$setInverse(i)
i
}
# Essentially it is just a rewrite of the examples
#with solve() instead of mean(), inverse instead of mean and i insead of m.