Skip to content

Commit 64a403d

Browse files
author
jjww2015
committed
submit the exercise function
1 parent 7f657dd commit 64a403d

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

Diff for: cachematrix.R

+46-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Matrix inversion is potentially time-consuming, particullarly when repeated
2+
## inversion must be performed on the same matrix. This function is to creates
3+
## a special "matrix" object that can cache its inverse If the inverse has
4+
## already been calculated (and the matrix has not changed), then this function
5+
##retrieve the inverse from the cache without repeating the operation.
36

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

69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
# Creates a special "matrix" object that can cache its inverse.
11+
#
12+
# Args:
13+
# x: One matrix used to initialize the cacheMatrix object.
14+
#
15+
# Returns:
16+
# An object that can cache the inverse of the input matrix.
17+
invx <- NULL
18+
set <- function(y) {
19+
x <<- y
20+
invx <<- NULL
21+
}
22+
get <- function() x
23+
setMatrixInverse <- function(invMatrix) invx <<- invMatrix
24+
getMatrixInverse <- function() invx
25+
list(set = set, get = get,
26+
setMatrixInverse = setMatrixInverse,
27+
getMatrixInverse = getMatrixInverse)
828
}
929

10-
11-
## Write a short comment describing this function
30+
## This function computes the inverse of the special "matrix" returned by
31+
## makeCacheMatrix above. If the inverse has already been calculated
32+
## (and the matrix has not changed), then cacheSolve should retrieve the
33+
## inverse from the cache.
1234

1335
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
36+
# Creates a special "matrix" object that can cache its inverse.
37+
#
38+
# Args:
39+
# x: matrix object that can cache its inverse.
40+
#
41+
# Returns:
42+
# the inverse of the matrix.
43+
44+
## Return a matrix that is the inverse of 'x'
45+
invx <- x$getMatrixInverse()
46+
if(!is.null(invx)) {
47+
message("getting cached inversed matrix")
48+
return(invx)
49+
}
50+
data <- x$get()
51+
invx <- solve(data, ...)
52+
x$setMatrixInverse(invx)
53+
invx
1554
}

0 commit comments

Comments
 (0)