File tree Expand file tree Collapse file tree 1 file changed +30
-6
lines changed Expand file tree Collapse file tree 1 file changed +30
-6
lines changed Original file line number Diff line number Diff line change 1
1
# # Put comments here that give an overall description of what your
2
2
# # functions do
3
3
4
- # # Write a short comment describing this function
5
-
4
+ # This function creates a special matrix which is really
5
+ # a list containing a function to
6
+ # 1. set the value of the matrix
7
+ # 2. get the value of the matrix
8
+ # 3. set the value of the inverse
9
+ # 4. get the value of the inverse
6
10
makeCacheMatrix <- function (x = matrix ()) {
7
-
11
+ inv <- NULL
12
+ set <- function (y ) {
13
+ x <<- y
14
+ inv <<- NULL
15
+ }
16
+ get <- function () x
17
+ setinverse <- function (inverse ) inv <<- inverse
18
+ getinverse <- function () inv
19
+ list (set = set , get = get , setinverse = setinverse ,
20
+ getinverse = getinverse )
8
21
}
9
22
10
23
11
- # # Write a short comment describing this function
12
-
24
+ # The following function calculates the inverse of the special "matrix"
25
+ # created with makeCacheMatrix.
26
+ # However, it first checks for an already cached inverse and skips
27
+ # the computation if it finds one.
13
28
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
29
+ # # Return a matrix that is the inverse of 'x'
30
+ inv < = x $ getinverse()
31
+ if (! is.null(inv )) {
32
+ message(" getting cached data!" )
33
+ return (inv )
34
+ }
35
+ data <- x $ get()
36
+ inv <- solve(data , ... )
37
+ x $ setinverse(inv )
38
+ inv
15
39
}
You can’t perform that action at this time.
0 commit comments