-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
68 lines (60 loc) · 2.12 KB
/
Copy pathcachematrix.R
File metadata and controls
68 lines (60 loc) · 2.12 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
63
64
65
66
67
68
# see "https://github.com/DanieleP/PA2-clarifying_instructions" for a
# clear explanation of each step from te example functions makeVector
# and cacheMean provided by the R Programming course
## Assignment 2
# Matrix inversion is usually a costly computation and there may be
# some benefit to caching the inverse of a matrix rather than compute
# it repeatedly
# The functions 'makeCacheMatrix' and 'cacheSolve' create a Matrix in
# cache and solves' the inverse of the matrix respectively.
## Example:
# x <- rbind(c(1, -1/4), c(-1/4, 1)) # creates a 2x2 matrix, see below:
#
#
# x = [,1] [,2]
# [1,] 1.00 -0.25
# [2,] -0.25 1.00
#
# executing x <- makeCacheMatrix(x) overwrites 'x' and saves the matrix
# into cache instead of working memory
#
# executing cacheSolve(x) retrieves te matrix from cache, calculates the
# inverse of x and saves the inverse back to cache (x$getsolve)
#
# x$getsolve()
# [,1] [,2]
# [1,] 1.0666667 0.2666667
# [2,] 0.2666667 1.0666667
#------------------------------------------------------------------------------
# please refer to the detailed example above,
# for further reference read:
# https://github.com/DanieleP/PA2-clarifying_instructions
# it's really clarifying!
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)
}
#------------------------------------------------------------------------------
# please refer to the detailed example above,
# for further reference read:
# https://github.com/DanieleP/PA2-clarifying_instructions
# it's really clarifying!
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
}