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
50 lines (44 loc) · 1.41 KB
/
cachematrix.R
File metadata and controls
50 lines (44 loc) · 1.41 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
## Assignment: Caching the Inverse of a Matrix
#"Matrix inversion is usually a costly computation and their may be some benefit to caching the inverse of a matrix rather than compute it repeatedly"
##Very similar to the example function makeVector, this function, creates a list containing a functions to
#set and get the value of the matrix
#set and get the value of the inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinverse <- function(inverse) m <<- inverse
getinverse <- function() m
lista = list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
return(lista)
}
##This function starts by checking if the inverse of the matrix already as been calculated.
#If true it returns the cached value for the inverse
#If not it calculates the inserse, caches it and returns it
cacheSolve <- function(x, ...) {
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
## Return a matrix that is the inverse of 'x'
return(m)
}
##You can run this to test it.
test_it = function(){
matr = replicate(1000, rnorm(1000))
matr_c_ready = makeCacheMatrix(matr)
message("running first time")
m1 = cacheSolve(matr_c_ready)
message("running second time")
m2 = cacheSolve(matr_c_ready)
message("end")
}