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
56 lines (34 loc) · 1012 Bytes
/
Copy pathcachematrix.R
File metadata and controls
56 lines (34 loc) · 1012 Bytes
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
## This function creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
minverse <- NULL
## Set the value of the matrix
setmatrix <- function(y) {
x <<- y
minverse <<- NULL
}
## Get the value of the matrix
getmatrix <- function() x
## Set the value of the inverse
setinverse <- function(solve) minverse <<- solve
## Get the value of the inverse
getinverse <- function() minverse
## Output
list(setmatrix = setmatrix, getmatrix = getmatrix,
setinverse = setinverse,
getinverse = getinverse)
}
## This function retrieves the inverse from makeCacheMatrix or computes the inverse of a matrix.
cacheSolve <- function(x, ...) {
## Retrieve the inverse matrix from makeCacheMatrix
minverse <- x$getinverse()
## If none is returned, compute the inverse matrix
if(!is.null(minverse)) {
message("getting cached data")
return(minverse)
}
m <- x$getmatrix()
minverse <- solve(m)
x$setinverse(minverse)
## Output
minverse
}