File tree 1 file changed +28
-7
lines changed
1 file changed +28
-7
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
1
+ # This function creates a special "matrix" object that can cache its inverse.
5
2
6
3
makeCacheMatrix <- function (x = matrix ()) {
4
+ # Although the 'set' function is not required in the description of the task
5
+ # there can be found a phrase in the description of cacheSolve function
6
+ # "(and the matrix has not changed)" implying that the matrix can be changed.
7
7
8
+ inv <- NULL
9
+ set <- function (y ) {
10
+ x <<- y
11
+ inv <<- NULL
12
+ }
13
+ get <- function () x
14
+ setinverse <- function (inv_2 )
15
+ inv <<- inv_2
16
+ getinverse <- function ()
17
+ inv
18
+ list (set = set , get = get , setinverse = setinverse , getinverse = getinverse )
8
19
}
9
20
10
-
11
- # # Write a short comment describing this function
21
+ # This function computes the inverse of the special "matrix" returned by
22
+ # makeCacheMatrix above.
23
+ # If the inverse has already been calculated (and the matrix has not changed),
24
+ # then the cachesolve should retrieve the inverse from the cache
12
25
13
26
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
27
+ inv <- x $ getinverse()
28
+ if (! is.null(inv )) {
29
+ message(" getting cached data" )
30
+ return (inv )
31
+ }
32
+ data <- x $ get()
33
+ inv <- solve(data , ... )
34
+ x $ setinverse(inv )
35
+ inv
15
36
}
You can’t perform that action at this time.
0 commit comments