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
41 lines (32 loc) · 1.17 KB
/
Copy pathcachematrix.R
File metadata and controls
41 lines (32 loc) · 1.17 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
## The two functions create a matrix object that can store along the matrix
## inverse. The inverse is calculated and stored with the cacheSolve function.
## The function makeCacheMatrix creates a new matrix type element
## that can store the inverse of the matrix along with the data values
makeCacheMatrix <- function(x = matrix()) {
inv <- matrix(ncol=3, nrow=3)
set <- function(y){
x<<-y
inv<<-NULL
}
get <- function()x
setinverse <- function(inverse)inv<<-inverse
getinverse <- function()inv
list(set = set, get = get,
setinverse = setinverse,
getinvers=getinverse)
}
## The cacheSolve function first checks if the inverse of the matrix
## Element was already calculated. If so, it will return the inverse
## the matrix, if not, it will calculate it and return it.
cacheSolve <- function(x = matrix(), ...) {
inv<- x$getinvers()
if(sum(!is.na(inv))>0){
message ("getting cached data")
return(inv)
}
data <- x$get()
print("calculating")
inv <- solve(data)
x$setinverse(inv)
inv
}