1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # This function creates a special "matrix" object
5
- # # that can cache its inverse.
1
+ # # This function creates a special "matrix" object that can cache its inverse.
6
2
7
3
makeCacheMatrix <- function (x = matrix ()) {
8
4
matrix <- NULL
9
- set <- function (y ) {
5
+ set <- function (y ) { # set is a function to set the value of inverse
10
6
x <<- y # # search through parent environment first for a value for y and assign to x
11
7
inverse <<- NULL # # set the inverse to null, unless it already exists
12
8
}
13
- get <- function () x
9
+ get <- function () x # get is a function to get the value of the matrix
14
10
setinverse <- function (inverse ) x <<- inverse # set the inverse value
15
11
getinverse <- function () x # get the inverse value
16
- list (set = set , get = get ,
12
+ list (set = set , get = get , # create a list with the values of each function
17
13
setinverse = setinverse ,
18
14
getinverse = getinverse )
19
- }
20
15
}
21
16
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
17
+ # # cacheSolve calculates the inverse of the “matrix” returned by makeCacheMatrix()
27
18
cacheSolve <- function (x , ... ) {
28
19
# # Return a matrix that is the inverse of 'x'
29
20
n <- x $ getinverse()
@@ -32,7 +23,7 @@ cacheSolve <- function(x, ...) {
32
23
return (n ) # return the cached inverted matrix if it already exists
33
24
}
34
25
data <- x $ get() # if we don't have a in existing inverted matrix, create one
35
- n <- solve(data , ... )
26
+ n <- solve(data , ... ) # use solve() to find the inverse
36
27
x $ setinverse(n )
37
- n
28
+ n # resturn the inverse
38
29
}
0 commit comments