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
30 lines (27 loc) · 1017 Bytes
/
cachematrix.R
File metadata and controls
30 lines (27 loc) · 1017 Bytes
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
## This R file contains 2 functions. One with 4 functions to set and retrieve matrix. Other method to cache and return cached matrix which will inturn call the get and set functions.
## Following function sets the inversed matrix and provides functions for set, get setinverse and getinverse
makeCacheMatrix <- function(x = matrix()) {
invmat <- NULL
set <- function(y) {
x <<- y
invmat <<- NULL
}
get <- function() x
setinverse <- function(x) invmat <<- x
getinverse <- function() invmat
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
# This function inverts the matrix using solve function and uses functions within the makeCacheMatrix. Returns cached matrix if available
cacheSolve <- function(x, ...) {
invmat <- x$getinverse()
if(!is.null(invmat)) {
message("getting cached data")
return(invmat)
}
data <- x$get()
invmat <- solve(data, ...)
x$setinverse(invmat)
invmat
}