Repository navigation
Expand file tree
/
Copy pathcachematrix.R
More file actions
62 lines (49 loc) · 1.92 KB
/
Copy pathcachematrix.R
File metadata and controls
62 lines (49 loc) · 1.92 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
55
56
57
58
59
60
61
62
## makeCacheMatrix functions creates cached value and
## implement set, get, setmatrix, and getmatrix functions
## cacheSolve functions obtain cached value if exists otherwise
## create inverse, cache it, and pass it as return.
## makeCachekMatrix take matrix as parameter and
## return list of functions
makeCacheMatrix <- function(x = matrix()) {
# Empty local variable to hold matrix
ma <- NULL
# Set current matrix variables to parameter and null out cached value
set <- function(y) {
x <<- y
ma <<- NULL
}
# Obtain matrix value
get <- function() x
# Set current value of matrix
setmatrix <- function(matr) ma <<- matr
# Get current value of matrix
getmatrix <- function() ma
list(set = set, get = get,
setmatrix = setmatrix,
getmatrix = getmatrix)
}
## Take makeCacheMatrix as parameter and return inverse matrix from cache/compute it.
cacheSolve <- function(x, ...) {
# Retrieve matrix stored in x
ma <- x$getmatrix()
#If matrix is already cached (not null) then return inverse matrix that is cached.
if(!is.null(ma)) {
message("getting cached data")
return(ma)
}
# Get matrix that is stored in ma variable using makeCacheMatrix$get function
matrixdata <- x$get()
# Invert matrix
ma <- solve(matrixdata, ...)
# Cache matrix so we can re-use matrix in next result.
x$setmatrix(ma)
## Return a matrix that is the inverse of 'x'
ma
}
##Create matrix
#x <- makeCacheMatrix(matrix(rnorm(5*5),nrow=5)) ; x$get()
##Get inverse matrix
#inversex <- cacheSolve(x) ; inversex ## Computing inverse of matrix and caching its result and pass result
#inversex <- cacheSolve(x) ; inversex ## Return Cached inverse
#x$set(matrix(rnorm(5*5),nrow=5))
#inversex <- cacheSolve(x) ; inversex ## Computing inverse of matrix and caching its result and pass result