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
62 lines (56 loc) · 1.73 KB
/
Copy pathcachematrix.R
File metadata and controls
62 lines (56 loc) · 1.73 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
52
53
54
55
56
57
58
59
60
61
62
## Programming Assignment 2:
## See example below
## Stores a matrix (it is assumed that the matrix supplied is always invertible)
## and its inverse
## Provides the following functions:
## - set(x): assign a matrix x to the object (and set its inverse to NULL)
## - get(): return the stored matrix
## - setinverse(inv): store the matrix 'inv' as the inverse matrix
## - getinverse(): return the stored inverse matrix
makeCacheMatrix <- function(x = matrix()) {
ix <- NULL
set <- function(y) {
x <<- y
ix <<- NULL
}
get <- function() x
setinverse <- function(inv) ix <<- inv
getinverse <- function() ix
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Function that needs an object built with the makeCacheMatrix (with an
## inversible matrix) and returns its inverse. If the inverse was calculated
## previously, a cached value will be returned
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
ix <- x$getinverse()
if(!is.null(ix)) {
message("getting cached data")
return(ix)
}
data <- x$get()
ix <- solve(data, ...)
x$setinverse(ix)
ix
}
## Example:
## > x <- matrix(c(1, 1, 0, 1, 0, 1, 0, 1, 0), nrow = 3, ncol = 3)
## mcm <- makeCacheMatrix(x)
## > mcm$get()
## [,1] [,2] [,3]
## [1,] 1 1 0
## [2,] 1 0 1
## [3,] 0 1 0
## > cacheSolve(mcm)
## [,1] [,2] [,3]
## [1,] 1 0 -1
## [2,] 0 0 1
## [3,] -1 1 1
## > cacheSolve(mcm)
## getting cached data
## [,1] [,2] [,3]
## [1,] 1 0 -1
## [2,] 0 0 1
## [3,] -1 1 1