Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ecf76fd

Browse files
committed
special "matrix" and cacheSolve for matrix inversion
Added code to stubs forked from R. D. Peng to finish the two functions makeCacheMatrix and cacheSolve for Assignment rdpeng#2.
1 parent e4eed41 commit ecf76fd

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

cachematrix.R

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,45 @@
22
## functions do
33

44
## Write a short comment describing this function
5+
# makeCacheMatix is a function that creates a list containing
6+
# functions that:
7+
# 1. sets the value of an invertible matrix. Note that it is
8+
# assumed that the matrix is invertible.
9+
# 2. gets the value of the invertible matrix.
10+
# 3. sets the value of the inverse of the matrix.
11+
# 4. gets the value of the inverse of the matrix.
512

613
makeCacheMatrix <- function(x = matrix()) {
7-
14+
m <- NULL
15+
set <- function(y) {
16+
x <<- y
17+
m <<- NULL
18+
}
19+
get <- function() x
20+
set.inverse <- function(solve) m <<- solve
21+
get.inverse <- function() m
22+
list(set = set, get = get,
23+
set.inverse = set.inverse,
24+
get.inverse = get.inverse)
825
}
926

10-
1127
## Write a short comment describing this function
28+
# This function calculates the inverse of the special matrix
29+
# that was created with 'makeCacheMatrix. If the inverse
30+
# has already been calculated, then it gets the cached value
31+
# of the inverse and reports this along with a message. If the
32+
# inverse has not been calculated, then it is calculated with
33+
# the function solve()
1234

1335
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
36+
## Return a matrix that is the inverse of 'x'
37+
m <- x$get.inverse()
38+
if(!is.null(m)) {
39+
message("getting cached data")
40+
return(m)
41+
}
42+
data <- x$get()
43+
m <- solve(data, ...)
44+
x$set.inverse(m)
45+
m
1546
}

0 commit comments

Comments
 (0)