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
46 lines (42 loc) · 1.02 KB
/
Copy pathcachematrix.R
File metadata and controls
46 lines (42 loc) · 1.02 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
setwd("~/Documents/rprogramming/ProgrammingAssignment2/")
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
makeCacheMatrix <- function(x = matrix()) {
#set default the inverse for the
k <- NULL
set <- function(y) {
#cache the values
x <<- y
k <<- NULL
}
#get functions
get <- function() x
#cache the inverse
setinv <- function(inv) k <<- inv
#return the inverse
getinv <- function() k
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
cacheSolve <- function(x, ...) {
#check if matrix fits the bill for a rectangular matrix
if (nrow(x$get()) != ncol(x$get())) {
message("Please use a rectangular matrix")
return()
}
## Return a matrix that is the inverse of x
i <- x$getinv()
if(!is.null(i)) {
message("Accessing Cache for Matrix")
return(i)
}
data <- x$get()
#solve the matrix
i <- solve(data, ...)
#set the inverse
x$setinv(i)
#return the inverse
i
}