Skip to content

Commit 7b8c1ec

Browse files
author
Jerome Jia
committed
submission for peer assessment
1 parent 7f657dd commit 7b8c1ec

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData

cachematrix.R

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## A set of functions that aids frequent access of results of matrix inversion operations,
2+
## best fit for matrices with large dimensions.
33

4-
## Write a short comment describing this function
4+
## Function to create a special matrix whose inversion is cached for subsequent access
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
i <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
i <<- NULL
11+
}
12+
get <- function() x
13+
setinv <- function(inv) i <<- inv
14+
getinv <- function() i
15+
list(set = set, get = get,
16+
setinv = setinv,
17+
getinv = getinv)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## Function to retrieve the inversion of any matrix created via makeCacheMatrix if already
22+
## cached, otherwise calculcates the inversion and cache it for the matrix
1223

1324
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
## returns NA if x is not properly created via makeCacheMatrix
26+
if(!"getinv" %in% names(x) | !"setinv" %in% names(x)) {
27+
warning("input matrix not wrapped by makeCacheMatrix")
28+
return(NA)
29+
}
30+
## Return a matrix that is the inverse of 'x'
31+
i <- x$getinv()
32+
if(!is.null(i)) {
33+
message("getting cached data")
34+
return(i)
35+
}
36+
data <- x$get()
37+
i <- solve(data, ...) %*% data
38+
x$setinv(i)
39+
i
1540
}

0 commit comments

Comments
 (0)