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 (58 loc) · 1.42 KB
/
cachematrix.R
File metadata and controls
62 lines (58 loc) · 1.42 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
## A pair of functions that cache the inverse of a matrix
## Creates a matrix object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
## Initialise the Inverse Matrix
inv <- NULL
## 1) Set the matrix value
set <- function(y) {
x <<- y
inv <<- NULL
}
## 2) Get the matrix value
get <- function(){
x
}
## 3) Set the value of the inverse Matrix
setinv <- function(inverse) {
inv <<- inverse
}
## 4) Get the Value of the inverse matrix
getinv <- function() {
inv
}
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
#Computing the inverse of a square matrix can be done with the solve function in R.
#For example, if X is a square invertible matrix, then solve(X) returns its inverse.
#inverse from the cache.
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
}
## Finally returns the inverse matrix
## Test Run:
##x = rbind(c(1,-1/4),c(10,-1/4))
##> m = makeCacheMatrix(x)
##> m$get()
##[,1] [,2]
##[1,] 1 -0.25
##[2,] 10 -0.25
##> cacheSolve(m)
##[,1] [,2]
##[1,] -0.1111111 0.1111111
##[2,] -4.4444444 0.4444444
##> cacheSolve(m)
##getting cached data
##[,1] [,2]
##[1,] -0.1111111 0.1111111
##[2,] -4.4444444 0.4444444
##>