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
32 lines (27 loc) · 907 Bytes
/
cachematrix.R
File metadata and controls
32 lines (27 loc) · 907 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
## A pair of functions that cache the inverse of a matrix
## Creates a special 'matrix', which is really a list containing a function to:
## set the matrix, get the matrix, set the inverse matrix, get the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
imat <- NULL
set <- function(y) {
x <<- y
imat <<- NULL
}
get <- function() x
setinverse <- function(inverse) imat <<- inverse
getinverse <- function() imat
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
}
## Calculates the mean of the special 'matrix' created with the above function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
imat <- x$getinverse()
if (!is.null(imat)) {
message("getting cached data")
return(imat)
}
mat <- x$get()
imat <- solve(mat)
x$setinverse(imat)
imat
}