Skip to content

Commit 3b76907

Browse files
committed
initial draft
1 parent 7f657dd commit 3b76907

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

cachematrix.R

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
31

4-
## Write a short comment describing this function
2+
3+
## This function creates a special "matrix" object that can cache its inverse.
54

65
makeCacheMatrix <- function(x = matrix()) {
76

7+
m <- NULL
8+
9+
set <- function(y) {
10+
x <<- y
11+
m <<- NULL
12+
}
13+
get <- function() x
14+
15+
setmatrix <- function(matrix) m <<- matrix
16+
getmatrix <- function() m
17+
18+
list(set = set, get = get,
19+
setmatrix = setmatrix,
20+
getmatrix = getmatrix)
21+
822
}
923

1024

11-
## Write a short comment describing this function
25+
## This function computes the inverse of the special
26+
## "matrix" returned by `makeCacheMatrix` above. If the inverse has
27+
## already been calculated (and the matrix has not changed), then
28+
## `cacheSolve` should retrieve the inverse from the cache.
1229

1330
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
31+
32+
## Return a matrix that is the inverse of 'x'
33+
34+
m <- x$getmatrix()
35+
if(!is.null(m)) {
36+
message("getting cached data")
37+
return(m)
38+
}
39+
data <- x$get()
40+
m <- solve(data, ...)
41+
x$setmatrix(m)
42+
m
43+
1544
}
45+
46+
# steps to test
47+
48+
# load makeCacheMatrix and cacheSolve
49+
50+
# m <- matrix(
51+
# c(3, 1, 5, 7),
52+
# nrow=2,
53+
# ncol=2,
54+
# byrow = TRUE)
55+
56+
# mc <- makeCacheMatrix(m)
57+
58+
# cacheSolve(mc)

0 commit comments

Comments
 (0)