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
33 lines (32 loc) · 971 Bytes
/
Copy pathcachematrix.R
File metadata and controls
33 lines (32 loc) · 971 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
## creates a special matrix object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
s <- NULL
## sets the matrix
set <- function(y) {
x <<- y
s <<- NULL
}
## gets the value of the matrix
get <- function() x
## saves the value of the solve
setSolve <- function(solve) s <<- solve
## gets the value of the inverse
getSolve <- function() s
list(set = set, get = get,
setSolve = setSolve,
getSolve = getSolve)
}
## gets the inversed matrix from a special object created by makeCacheMatrix
cacheSolve <- function(x, ...) {
## if it is available gets cached matrix saved in x and returns its inverse saved as s
s <- x$getSolve()
if(!is.null(s)) {
message("getting cached data")
return(s)
}
## otherwise computes the matrix from x, saves it to the cache and returns s
data <- x$get()
s <- solve(data, ...)
x$setSolve(s)
s
}