Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3209dd8

Browse files
committed
[cachematrix] Solve for Programming Assignment 2.
1 parent 7f657dd commit 3209dd8

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

cachematrix.R

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
## There are two functions in this file.
2+
##
3+
## makeCacheMatrix - puts the matrix and return the "matrix" object
4+
## that is the list with special get/set functions.
5+
##
6+
## cacheSolve - computes the inverse of the special "matrix" returned
7+
## by makeCacheMatrix. If the inverse has already been calculated
8+
## (and the matrix has not changed), then the cachesolve should
9+
## retrieve the inverse from the cache.
710

11+
makeCacheMatrix <- function(m = matrix()) {
12+
inv <- NULL
13+
set <- function(y) {
14+
m <<- y
15+
inv <<- NULL
16+
}
17+
get <- function() { m }
18+
setinv <- function(value) { inv <<- value }
19+
getinv <- function() { inv }
20+
list(set = set, get = get,
21+
setinv = setinv,
22+
getinv = getinv)
823
}
924

10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
25+
cacheSolve <- function(m, ...) {
26+
inv <- m$getinv()
27+
if(!is.null(inv)) {
28+
message("getting cached data")
29+
return(inv)
30+
}
31+
matrix <- m$get()
32+
inv <- solve(matrix, ...)
33+
m$setinv(inv)
34+
inv
1535
}
36+

0 commit comments

Comments
 (0)