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
36 lines (32 loc) · 903 Bytes
/
cachematrix.R
File metadata and controls
36 lines (32 loc) · 903 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
35
36
## Put comments here that give an overall description of what your
## functions do
## create "object" of a type MATRIX
# add four "methods" - get, set, getinv, setinv
# parameter - may be skipped or existing matrix
makeCacheMatrix <- function(mt = matrix()) {
mt.inverse <- NULL
set <- function(y) {
mt <<- y
mt.inverse <<- NULL
}
get <- function() mt
setinv <- function(inv) mt.inverse <<- inv
getinv <- function() mt.inverse
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## Returns inverse of the parameter matrix
# assumption - the given matrix is invertible
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
#check if cache does not exist - then create it
if (is.null(inv)){
message("creating cache first time")
mat <- x$get()
inv <- solve(mat)
x$setinv(inv)
}
inv
}