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
62 lines (43 loc) · 1.67 KB
/
cachematrix.R
File metadata and controls
62 lines (43 loc) · 1.67 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
# Function to generate list of functions to store a matrix and its
# inverse in cache and retrive them
makeCacheMatrix <- function(x = matrix()) {
invMatrix <- NULL
# Function to store matrix in cache
# When new matrix is stored, inverse matrix of the old matrix is deleted
set <- function(y) {
x <<- y
invMatrix <<- NULL
}
# Function to retrieve matrix stored in cache
get <- function() x
# Function to store inverse matrix in cache
setInverse <- function(inverse) invMatrix <<- inverse
# Function to retrieving inverse matrix from cache
getInverse <- function() invMatrix
# Returning the list of functions
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Write a short comment describing this function
# Function to return inverse matrix of a matrix stored in cache, either by
# computing the inverse or retrieving it from cache
# Takes vector returned by makeCacheMatrix as input
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# Retrieving inverse matrix stored in cache
invMatrix <- x$getInverse()
# If inverse matrix is stored in cache, returning the same
if(!is.null(invMatrix)) {
print("getting cached data")
return(invMatrix)
}
# If inverse matrix is not stored, retrieving the matrix from cache
matrix <- x$get()
# Computing the inverse of matrix and storing the inverse in cache
invMatrix <- solve(matrix, ...)
x$setInverse(invMatrix)
# Returning the inverse matrix
invMatrix
}