10000 Completing assignment #3. · cschooley/ProgrammingAssignment2@3d7da8a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d7da8a

Browse files
committed
Completing assignment rdpeng#3.
1 parent 7f657dd commit 3d7da8a

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

cachematrix.R

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Matrix inversion is usually a costly computation and there may be some benefit to caching
2+
##the inverse of a matrix rather than compute it repeatedly (there are also alternatives to
3+
##matrix inversion that we will not discuss here). Your assignment is to write a pair of
4+
##functions that cache the inverse of a matrix.
35

4-
## Write a short comment describing this function
6+
## This function creates (returns) a special "matrix" object that can cache its inverse.
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
i <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
i <<- NULL
13+
}
14+
get <- function() x
15+
setinv <- function(solve) i <<- solve
16+
getinv <- function() i
17+
list(set = set, get = get,
18+
setinv = setinv,
19+
getinv = getinv)
820
}
921

1022

11-
## Write a short comment describing this function
23+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
24+
## If the inverse has already been calculated (and the matrix has not changed),
25+
## then the cachesolve should retrieve the inverse from the cache.
1226

1327
cacheSolve <- function(x, ...) {
1428
## Return a matrix that is the inverse of 'x'
29+
i <- x$getinv()
30+
if(!is.null(i)) {
31+
message("getting cached data")
32+
return(i)
33+
}
34+
data <- x$get()
35+
i <- solve(data, ...)
36+
x$setinv(i)
37+
i
1538
}
39+

0 commit comments

Comments
 (0)
0