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
55 lines (46 loc) · 1.63 KB
/
cachematrix.R
File metadata and controls
55 lines (46 loc) · 1.63 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
## makeCacheMatrix involves several commands used to set, get,
## and solve for the inversion of a Matrix and will be used in
## cacheSolve funtion listed below. The <<- operator is used
## to assign a value to an object in an environment
## that is different from the current environment.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
## set the matrix arguments
set <- function(y=matrix()) {
x <<- y
m <<- NULL
}
## get matrix arguments
get <- function() x
## functions used in cacheSolve
setsolve <- function(solve) m <<- solve
getsolve <- function() m
## list of commands created by makeCacheMatrix
list(set = set, get = get, setsolve = setsolve,
getsolve = getsolve)
}
## Write a short comment describing this function
## The following function calculates the inverse of a matrix created using
## makeCacheMatrix (above). The function, cacheSolve, will first check to
## see if an inverse of the matrix has already been calculated. If so, it
## gets the inverse from the cache value and skips the computation.
cacheSolve <- function(x = matrix(), ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getsolve()
## if inverse exist skip computation and return
if(!is.null(m)) {
message("getting cached data")
return(m)
}
## gets matrix to br inverted
data <- x$get()
## inverts matrix
m <- solve(data, ...)
## Set the inverse value
x$setsolve(m)
m
}
}