File tree Expand file tree Collapse file tree 1 file changed +26
-10
lines changed Expand file tree Collapse file tree 1 file changed +26
-10
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
+ # # Create method of storing matrices and calculating their inversions that only
2
+ # # calculates the inversion for a given matrix once.
5
3
4
+ # # Cached matrix data type (functions to store/retrieve matrix/inversion).
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
-
6
+ m = NULL
7
+ set <- function (y ) {
8
+ x <<- y
9
+ m <<- NULL
10
+ }
11
+ get <- function () x
12
+ setinverse <- function (inverse ) m <<- inverse
13
+ getinverse <- function () m
14
+ list (set = set , get = get ,
15
+ setinverse = setinverse ,
16
+ getinverse = getinverse )
8
17
}
9
18
10
-
11
- # # Write a short comment describing this function
12
-
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
19
+ # # Calculate inversion of matrix stored in cached matrix data type if needed.
20
+ cacheSolve <- function (x ) {
21
+ # # Return a matrix that is the inverse of 'x' (calculate if necessary).
22
+ i <- x $ getinverse()
23
+ if (! is.null(i )) {
24
+ message(' getting cached data' )
25
+ return (i )
26
+ }
27
+ data <- x $ get()
28
+ i <- solve(data )
29
+ x $ setinverse(i )
30
+ i
15
31
}
You can’t perform that action at this time.
0 commit comments