forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
43 lines (35 loc) · 1.01 KB
/
cachematrix.R
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
## Put comments here that give an overall description of what your
## functions do
## Function to create a cacheMatrix object,
## used to cache the value of a matrix and it's inverse
makeCacheMatrix <- function(x = matrix()) {
inversed_matrix <- NULL
set <- function(y) {
x <<- y
inversed_matrix <<- NULL
}
get <- function() {
return(x)
}
setInverse <- function(m){
inversed_matrix <<- m
}
getInverse <- function(){
return(inversed_matrix)
}
return(list(set = set, get = get, setInverse = setInverse, getInverse = getInverse))
}
## Function that retrieves and returns the inverse of a matrix in a cacheMatrix object if stored,
## or that calculates and stores the inverse of a matrix stored in a cacheMatrix object.
cacheSolve <- function(x, ...) {
inversedMatrix <- x$getInverse()
if( !is.null(inversedMatrix) ){
message("returning cached data")
}
else{
originalMatrix <- x$get()
inversedMatrix <- solve(originalMatrix)
x$setInverse(inversedMatrix)
}
return(inversedMatrix)
}