File tree Expand file tree Collapse file tree 1 file changed +29
-8
lines changed Expand file tree Collapse file tree 1 file changed +29
-8
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
+ # # Pair of functions to cache a matrix inverse calculation
2
+ # example of use:
3
+ # m = matrix(rnorm(100),nrow=10)
4
+ # m = makeCacheMatrix(m)
5
+ # mi = cacheSolve(m)
5
6
7
+ # # Creates a special "matrix" object that can cache its inverse
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
-
9
+ m <- NULL
10
+ set <- function (y ) {
11
+ x <<- y
12
+ m <<- NULL
13
+ }
14
+ get <- function () x
15
+ setinverse <- function (inverse ) m <<- inverse
16
+ getinverse <- function () m
17
+ list (set = set , get = get ,
18
+ setinverse = setinverse ,
19
+ getinverse = getinverse )
8
20
}
9
21
10
22
11
- # # Write a short comment describing this function
12
-
23
+ # # Computes the inverse of the special "matrix" returned by makeCacheMatrix
24
+ # # f the inverse has already been calculated (and the matrix has not changed),
25
+ # # then cacheSolve should retrieve the inverse from the cache.
13
26
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
27
+ m <- x $ getinverse()
28
+ if (! is.null(m )) {
29
+ message(" getting cached data" )
30
+ return (m )
31
+ }
32
+ data <- x $ get()
33
+ m <- solve(data , ... )
34
+ x $ setinverse(m )
35
+ m
15
36
}
You can’t perform that action at this time.
0 commit comments