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
51 lines (35 loc) · 1.4 KB
/
Copy pathcachematrix.R
File metadata and controls
51 lines (35 loc) · 1.4 KB
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
44
45
46
47
48
49
50
51
## The program has two functions makecachematrix and cache solve
## make cachematrix creates a list of 4 functions - set,get,setinv and getinv
## cachesolve computes the inverse if it has not been computed before and saves it using setinv and if it was computed before, it pulls the value from the cache
## make cachematrix creates a list of 4 functions - set,get,setinv and getinv
## set - sets the data
## get - pulls the data
## setinv - sets the inverse matrix
## getinv- pulls the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
inv<- NULL
set <- function(y)
{
x<<- y
inv= NULL
}
get<- function() x
setinv <- function(inverse) inv<<- inverse
getinv<- function() inv
list(set=set,get=get,setinv=setinv,getinv=getinv)
}
## cachesolve gets the variable inv first.
## if its null, it computes the inverse of the matrix, stores using setinv and prints the inverse
## if its not null, it pulls the inverse from cahce using getinv
cacheSolve <- function(x, ...) {
inv <- x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinv(inv)
inv
## Return a matrix that is the inverse of 'x'
}