8000 programming assignment2 · mksarnala/ProgrammingAssignment2@212ce01 · GitHub
[go: up one dir, main page]

Skip to content

Commit 212ce01

Browse files
committed
programming assignment2
1 parent 7f657dd commit 212ce01

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

cachematrix.R

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
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-
1+
# makeCacheMatrix creates a list containing a function to
2+
#1. set the value of the matrix
3+
#2. get the value of the matrix
4+
#3. set the value of inverse of the matrix
5+
#4. get the value of inverse of the matrix
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inv <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
inv <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) inv <<- inverse
14+
getinverse <- function() inv
15+
list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
816
}
9-
10-
11-
## Write a short comment describing this function
12-
17+
# The following function returns the inverse of the matrix. It first checks if
18+
# the inverse has already been computed. If so, it gets the result and skips the
19+
# computation. If not, it computes the inverse, sets the value in the cache via
20+
# setinverse function.
21+
# This function assumes that the matrix is always invertible.
1322
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
23+
inv <- x$getinverse()
24+
if(!is.null(inv)) {
25+
message("getting cached data.")
26+
return(inv)
27+
}
28+
data <- x$get()
29+
inv <- solve(data)
30+
x$setinverse(inv)
31+
inv
32+
}

0 commit comments

Comments
 (0)
0