forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
35 lines (29 loc) · 1013 Bytes
/
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
## These functions help store the inverse of a matrix in cache such that the inverse would not have to be calculated again
## This funciton takes as an argument one matrx and if nothing is given then creates one as default and returns a list
## of methods from which the inverse and matrix can be retreived and set
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setinverse <- function(inverse) i <<- inverse
getinverse <- function() i
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function returns the inverse of a matrix created by makeCacheMatrix function by calculating it if nothing is stored
## in cache or just directly retrieving it from cache if it exists.
cacheSolve <- function(x, ...) {
i <- x$getinverse()
if(!is.null(i)){
message("getting cached data")
return (i)
}
data <- x$get()
i <-solve(data,...)
x$setinverse(i)
i
}