forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
42 lines (36 loc) · 1.28 KB
/
cachematrix.R
File metadata and controls
42 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
## Caching the inverse of a matrix
## This function is aimed to save the time and cost of calculating a
## inverse of a matrix repeatedly by caching the inverse for future use.
## makeCacheMatrix Computes the inverse of a special matrix
makeCacheMatrix <- function(x = matrix()) {
## if a matrix is called without a method
m <- NULL
## set the value of the matrix
set <- function (y){
x <<- y
m <<- NULL
}
## get the value of the matrix
get <- function()x
## set the value of inverse
## get the value of inverse
setInverse <- function(inverse) m <<- inverse
getInverse <- function() m
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## cache the inverse of matrix
## cacheSolve function retrieves the inverse of the matrix from the
## cache,if the inverse has already been calculated by makeCacheMatrix.
## Otherwise, it computes the inverse by function solve(x).
cacheSolve <- function(x, ...) {
m <- x$getInverse()
if(!is.null(m)){
message ("getting cached data")
return(m)
}
m <- solve(x$get())
x$setInverse(m)
m
}