forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
44 lines (40 loc) · 1.18 KB
/
cachematrix.R
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
## Put comments here that give an overall description of what your
## functions do
## Set a specialized object for storing a matrix and some functions
makeCacheMatrix <- function(x = matrix()) {
## initialize inverse matrix
inverse <- NULL
## set matrix
set <- function(y) {
x <<- y
inverse <<- NULL
}
## get matrix
get <- function() x
## set inverse matrix
setinverse <- function(i) inverse <<- i
## get innverse matrix
getinverse <- function() inverse
## put functions to list object
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Look for and return inverse matrix if already exists or compute and store inverse matrix
cacheSolve <- function(x) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getinverse()
## if inverse matrix already exists in 'x' then print message and return inverse matrix
if(!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
## if inverse matrix doesn't exist then get matrix
data <- x$get()
## compute inverse matrix
inverse <- solve(data)
## set inverse matrix in 'x'
x$setinverse(inverse)
## return inverse matrix
inverse
}