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
68 lines (49 loc) · 1.73 KB
/
cachematrix.R
File metadata and controls
68 lines (49 loc) · 1.73 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
56
57
58
59
60
61
62
63
64
65
66
67
68
# Set up example matrix
mat1 = c(1,2,3,4)
mat1 = matrix(mat1,nrow=2,ncol=2,byrow=TRUE)
## Fxn 1: creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
# Setting up inverse matrix as null
m <- NULL
set <- function(matrix) { # Fxn 1 for setting value of matrix ("set")
x <<- matrix # Set x as y within makeVector fxn env
m <<- NULL # Overwrite m in makeVector fxn env with NULL
} # End of set fxn
get <- function() { # Fxn 2 for getting value of vector ("get")
x } # Print matrix
setInv <- function(solve) { # Fxn 3 for setting value of vector
m <<- solve }
getInv <- function() { # Fxn 4 for getting value of mean
m } # Print inverse matrix
list(set = set, get = get,
setInv = setInv,
getInv = getInv) # Creating list of output of 4 fxns
}
## Fxn 2: computes inverse of matrix
cacheSolve <- function(x, ...) { # ... means we can add extra arguments (unused)
m <- x$getInv() # Pull mean from above function
if(!is.null(m)) { #If m exists
message("getting cached data")
return(m) # spit out m
}
data <- x$get() # Get X matrix
m <- solve(data) # Calc inverse with matrix mult
x$setInv(m) # Set the object to be inverse
m # Return matrix
}
makeCacheMatrix(mat1)
cacheSolve(mat1)
# Extra version: do not use
# cacheSolve <- function(x, ...) {
# m <- x$getsolve() # Pull mean from above function
# if(is.null(m)) { # If m does not exist
# message("calculating inverse")
# solve(m) # Att: calc inv here
# return(m)
# }
# if(!is.null(m)) { # If m exists
# message("getting cached data")
# return(m) # spit out m
# }
# ## Return a matrix that is the inverse of 'x'
# }