Skip to content

Commit 14237af

Browse files
committed
committing solution to assignment rdpeng#2
1 parent 7f657dd commit 14237af

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+
## These two functions work very similarly to how the example functions for this programming assignment
2+
## (that is, makeVector and cacheMean) work
33

4-
## Write a short comment describing this function
4+
## makeCacheMatrix creates a special "matrix" which is really a list containing functions to:
5+
## 1- set the value of the matrix
6+
## 2- get the value of the matrix
7+
## 3- set the value of the inverse
8+
## 4- get the value of the inverse
59

610
makeCacheMatrix <- function(x = matrix()) {
7-
11+
i <- NULL
12+
set <- function(y) {
13+
x <<- y
14+
i <<- NULL
15+
}
16+
get <- function() x
17+
setinverse <- function(inv) i<<- inv
18+
getinverse <- function() i
19+
list(set= set, get = get, setinverse= setinverse, getinverse= getinverse)
820
}
921

1022

11-
## Write a short comment describing this function
23+
## cacheSolve calculates the inverse of a special "matrix" created with the above function.
24+
## It first checks to see if the inverse has already been calculated, and if so, returns the
25+
## inverse value from the cache and skips the computation. Otherwise, it calculates the inverse
26+
## of the matrix - using solve(x)- and sets the value of the cached inverse via the setinverse function.
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)