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
34 lines (29 loc) · 888 Bytes
/
cachematrix.R
File metadata and controls
34 lines (29 loc) · 888 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
31
32
33
34
## This function create new datatype cacheMatrix
## new data type is contructed using matrix as arg
## it has 4 functions setmtx , getmtx , setinv , getinv
## it returns list of 4 functions
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
m <- x
setmtx <- function(x) {
m <<- x;
inv <<- NULL;
}
getmtx <- function() return(m);
setinv <- function(x) inv <<- x;
getinv <- function() return(inv);
return(list(setmtx = setmtx, getmtx = getmtx, setinv = setinv, getinv = getinv))
}
## This function first checks if inverse of matrix is calculated
## If yes then it returns that ans
## Else it calculates inverse of matrix
## saves inverse in cacheMatrix
## and returns inverse
cacheSolve <- function(x, ...) {
if(!is.null(x$getinv())) {
message("using cached data")
return(x$getinv())
}
x$setinv(solve(x$getmtx()))
return(x$getinv())
}