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
40 lines (36 loc) · 1.28 KB
/
Copy pathcachematrix.R
File metadata and controls
40 lines (36 loc) · 1.28 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
### Function makeCacheMatrix creates a list with data/methods to store matrix x;
### data includes the matrix x itself, was it inverted (flag m, inverted if m is not NULL),
### and internal functions to operate the matrix: set, get, setInv, getInv.
### If necessary, the function cacheSolve either calculates the inverse matrix (if it hasn't been calculated
### already) and saves it, or returns the saved inverse matrix
### function makeCacheMatrix
### * Sets information about matrix x and flag m (if m is NULL it means matrix was not inverted);
### * Contains internal functions to operate the matrix: set, get, setInv, getInv.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setInv <- function(val) m <<- val
getInv <- function() m
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
### Function cacheSolve
### Checks if flag m is null, then calculates the inverse matrix and saves it,
### otherwise outputs already saved inverted matrix from m
### and prints the message "getting cached data"
cacheSolve <- function(x, ...) {
m <- x$getInv()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setInv(m)
m
}