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
35 lines (31 loc) · 1.04 KB
/
Copy pathcachematrix.R
File metadata and controls
35 lines (31 loc) · 1.04 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
## Put comments here that give an overall description of what your
## functions do
## This function generates a matrix that caches the inverse when it is calculated by cacheSolve.
## The function is called with an invertible matrix m as argument
## For a cached matrix x, use x$get() to retrieve the matrix and x$set(new_matrix) to change the values
## Other functions are helper functions for cacheSolve.
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setinv <- function(inv) i <<- inv
getinv <- function() i
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## This function calculates or retrieves the inverse of a cached matrix.
## The argument of the function is a cached matrix x generated by makeCacheMatrix(input_matrix)
## cacheSolve(x) returns the inverse of input_matrix
cacheSolve <- function(x, ...) {
i <- x$getinv()
if(!is.null(i)){
message("getting cached data")
return(i)
}
mat <- x$get()
i <- solve(mat)
x$setinv(i)
i
}