Repository navigation
Expand file tree
/
Copy pathcachematrix.R
More file actions
54 lines (42 loc) · 1.41 KB
/
Copy pathcachematrix.R
File metadata and controls
54 lines (42 loc) · 1.41 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
## Put comments here that give an overall description of what your
## functions do
## Together makeCacheMatrix and cacheSolve take in a matrix, create and inverse matrix
## solve the inverse matrix and cache the solution for future use.
## makeCacheMatrix takes in a matrix and return 4 functions: set, get, setinv and getinv
## set: sets the value of the matrix
## get: returns the value of the matrix
## setinv: sets the value of the inverse matrix
## getinv: gets the value of the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
# returns x as a matrix
get <- function() x
# calculates the inverse of x and puts it in the cache
setInverse <- function(inverse) m <<- inverse
# takes the inverse value
getInverse <- function() m
#returns all the functions
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## this function looks to see if function is in the cache and returns it
## otherwise calculates it
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse()
# if the inverse is present in the cache, return it
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data)
x$setInverse(m)
#returns it!
m
}