Skip to content

Commit b22d55d

Browse files
committed
added makeCacheMatrix and cacheSolve
1 parent 7f657dd commit b22d55d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

.gitignore

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

cachematrix.R

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## The following functions allow you to create a matrix and cache the inverse of a
2+
## matrix for later reference if it is available
33

4-
## Write a short comment describing this function
4+
## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
55

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
}
917

1018

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

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
## Return a matrix that is the inverse of 'x'
25+
inv <- x$getinverse()
26+
if (!is.null(inv)) {
27+
message("getting cached data")
28+
return(inv)
29+
}
30+
message('still null')
31+
data <- x$get()
32+
inv <- solve(data)
33+
x$setinverse(inv)
34+
inv
1535
}

0 commit comments

Comments
 (0)