Skip to content

Commit 2cdbf9b

Browse files
committed
my programming assignment
1 parent 7f657dd commit 2cdbf9b

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

cachematrix.R

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## This is a pair of functions that cache the inverse of a matrix.
32

4-
## Write a short comment describing this function
3+
## makeCacheMatrix creates a special "matrix" object that can cache its inverse.
4+
## First you set the matrix, then you get the matrix,
5+
## then you set the inverse, then you get the inverse!
56

6-
makeCacheMatrix <- function(x = matrix()) {
77

8+
makeCacheMatrix <- function(x = matrix()) {
9+
m = NULL
10+
set = function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
get = function() x
15+
setinv = function(inverse) m <<- inverse
16+
getinv = function() m
17+
list(set=set, get=get, setinv=setinv, getinv=getinv)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## cacheSolve computes the inverse of the special "matrix" returned by makeCacheMatrix above.
22+
## If the inverse has already been calculated (and the matrix has not changed), then the
23+
## cachesolve should retrieve the inverse from the cache.
1224

1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
m = x$getinv()
27+
if (!is.null(m)){
28+
message("getting cached data")
29+
return(m)
30+
}
31+
mydata = x$get()
32+
m = solve(mydata, ...)
33+
x$setinv(m)
34+
m
1535
}

0 commit comments

Comments
 (0)