Skip to content

Commit 9944e72

Browse files
committed
Commit rdpeng#1
1 parent 7f657dd commit 9944e72

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

cachematrix.R

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## ProgrammingAssignment2
32

4-
## Write a short comment describing this function
3+
## This function creates a "matrix" in that it makes a list containing functions
4+
## to set and get the value of the matrix and its inverse
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
m <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(inverse) m <<- inverse
14+
getinverse <- function() m
15+
list(set = set,
16+
get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
819
}
920

10-
11-
## Write a short comment describing this function
21+
## This function first checks to see if the inverse of the specified matrix has
22+
## been stored. If so it skips the calculation, otherwise it calculates the
23+
## inverse itself
1224

1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
m <- x$getinverse()
27+
if (!is.null(m)) {
28+
message("getting cached data")
29+
return(m)
30+
}
31+
data <- x$get()
32+
m <- solve(data, ...)
33+
x$setinverse(m)
34+
m
1535
}
36+
## Return a matrix that is the inverse of 'x'
37+
38+
## Test for yourself here
39+
40+
A <- matrix(c(1,2,3,4),2,2)
41+
A1 <- makeCacheMatrix(A)
42+
cacheSolve(A1) ## Run this line twice to get the message

0 commit comments

Comments
 (0)