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
51 lines (44 loc) · 1.39 KB
/
cachematrix.R
File metadata and controls
51 lines (44 loc) · 1.39 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
##
## Set of functions which create a matrix cacheing object to cache results of
## inverting a matrix - solve(x) for computationally long matrix operations
##
## Usage:
## > # Create square matrix that you want to solve
## > n = 4
## > squarematrix <- matrix(rnorm(n*n), n, n)
## > # create caching matrix object
## > x <- makeCacheMatrix(squarematrix)
## > # calculate inverted matrix
## > View(cacheSolve(x))
## > # running again, it fetches results from cache
## > View(cacheSolve(x))
## getting cached data
## >
## Sets up caching object.
## first argument is square matrix for caching operations
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setsolve <- function(solve) m <<- solve
getsolve <- function() m
list(set = set, get = get, setsolve=setsolve, getsolve=getsolve)
}
## Solves and caches square matrix inversion
## subsequent runs are cached
## first argument is object as initialised from 'makeCacheMatrix' function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setsolve(m)
m
}