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
53 lines (40 loc) · 1.21 KB
/
cachematrix.R
File metadata and controls
53 lines (40 loc) · 1.21 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
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
makeCacheMatrix <- function(x = matrix()) {
# Let's check if we have correct input
if (!is.matrix(x)) {
stop("Please provide a matrix")
}
inverted_x <- NULL
set <- function(y) {
x <<- y
inverted_x <<- NULL
}
# Functions for getting and setting cached inv. matrix value
get <- function() x
# Inversing the matrix using build in solve() function in R
set.inverse <- function(solve) inverted_x <<- solve
get.inverse <- function() inverted_x
list(
set = set,
get = get,
set.inverse = set.inverse,
get.inverse = get.inverse)
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverted_x <- x$get.inverse()
# Do we have cached matrix available?
if(!is.null(inverted_x)) {
message("Getting cached inverse matrix")
return(inverted_x)
}
# Let's create inverted matrix in case
# there's no cached matrix available.
matrix_to_invert <- x$get()
inverted_x <- solve(matrix_to_invert)
x$set.inverse(inverted_x)
inverted_x
}