File tree Expand file tree Collapse file tree 1 file changed +29
-6
lines changed Expand file tree Collapse file tree 1 file changed +29
-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
4
+ # # This function creates a special "matrix" object
5
+ # # that can cache its inverse.
5
6
6
7
makeCacheMatrix <- function (x = matrix ()) {
7
-
8
+ matrix <- NULL
9
+ set <- function (y ) {
10
+ x <<- y # # search through parent environment first for a value for y and assign to x
11
+ inverse <<- NULL # # set the inverse to null, unless it already exists
12
+ }
13
+ get <- function () x
14
+ setinverse <- function (inverse ) x <<- inverse # set the inverse value
15
+ getinverse <- function () x # get the inverse value
16
+ list (set = set , get = get ,
17
+ setinverse = setinverse ,
18
+ getinverse = getinverse )
19
+ }
8
20
}
9
21
10
-
11
- # # Write a short comment describing this function
12
-
22
+ # # This function computes the inverse of the special
23
+ # # "matrix" returned by `makeCacheMatrix` above.
24
+ # # Computing the inverse of a square matrix can be done with the `solve`
25
+ # # function in R.
26
+ # # n is the inverted matrix
13
27
cacheSolve <- function (x , ... ) {
14
28
# # Return a matrix that is the inverse of 'x'
15
- }
29
+ n <- x $ getinverse()
30
+ if (! is.null(n )) {
31
+ message(" getting cached data" )
32
+ return (n ) # return the cached inverted matrix if it already exists
33
+ }
34
+ data <- x $ get() # if we don't have a in existing inverted matrix, create one
35
+ n <- solve(data , ... )
36
+ x $ setinverse(n )
37
+ n
38
+ }
You can’t perform that action at this time.
0 commit comments