Skip to content

Commit 4417514

Browse files
authored
Merge pull request rdpeng#1 from ganeshsimat/ganeshsimat-patch-1
Update cachematrix.R
2 parents 7f657dd + 7ba0a05 commit 4417514

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

cachematrix.R

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse
32

4-
## Write a short comment describing this function
3+
## cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
4+
## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve
5+
## the inverse from the cache
6+
7+
## Computing the inverse of a square matrix can be done with the solve function in R.
8+
## For example, if X is a square invertible matrix, then solve(X) returns its inverse.
59

610
makeCacheMatrix <- function(x = matrix()) {
11+
I <- NULL
12+
set <- function(y) {
13+
x <<- y
14+
I <<- NULL
15+
}
16+
get <- function() x
17+
setinverse <- function(solve) I <<- solve
18+
getinverse <- function() I
19+
list(set = set, get = get,
20+
setinverse = setinverse,
21+
getinverse = getinverse)
722

823
}
924

@@ -12,4 +27,13 @@ makeCacheMatrix <- function(x = matrix()) {
1227

1328
cacheSolve <- function(x, ...) {
1429
## Return a matrix that is the inverse of 'x'
30+
I <- x$getinverse()
31+
if(!is.null(I)) {
32+
message("getting cached data")
33+
return(I)
34+
}
35+
data <- x$get()
36+
I <- solve(data, ...)
37+
x$setinverse(I)
38+
I
1539
}

0 commit comments

Comments
 (0)