File tree Expand file tree Collapse file tree 2 files changed +70
-2
lines changed Expand file tree Collapse file tree 2 files changed +70
-2
lines changed Original file line number Diff line number Diff line change
1
+ getwd()
2
+ setwd("~/Documents/GitHub/ProgrammingAssignment2")
3
+ }
4
+ }
5
+ ls
6
+ makeCacheMatrix
7
+ source("cachematrix.R")
8
+ makeCacheMatrix(c(10))
9
+ x <- matrix(1:6, 2, 3)
10
+ x
11
+ makeCacheMatrix(x)
12
+ cacheSolve(x)
13
+ cacheSolve(x, makeCacheMatrix(x))
14
+ View(cacheSolve)
15
+ View(cacheSolve)
16
+ View(cacheSolve)
17
+ View(makeCacheMatrix)
18
+ makeCacheMatrix <- function(x = matrix()) {
19
+ ## functions do
20
+ ## Write a short comment describing this function
21
+ makeCacheMatrix <- function(x = matrix()) {
22
+ inv <- NULL
23
+ set <- function(y) {
24
+ x <<- y
25
+ inv <<- NULL
26
+ }
27
+ get <- function() x
28
+ setinverse <- function(inv) inv <<- inverse
29
+ getinverse <- function() inv
30
+ list(set = set, get = get,
31
+ setinverse = setinverse,
32
+ getinverse = getinverse)
33
+ }
34
+ ## Write a short comment describing this function
35
+ cacheSolve <- function(x, ...) {
36
+ ## Return a matrix that is the inverse of 'x'
37
+ inv <- x$getinverse()
38
+ if(!is.null(inv)) {
39
+ message("getting cached data")
40
+ return(inv)
41
+ }
42
+ data <- x$get()
43
+ inv <- solve(data, ...)
44
+ x$setinverse(inv)
45
+ inv
46
+ }
47
+ exit
48
+ ;
49
+ ;;
Original file line number Diff line number Diff line change 4
4
# # Write a short comment describing this function
5
5
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ inv <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ inv <<- NULL
11
+ }
12
+ get <- function () x
13
+ setinverse <- function (inv ) inv <<- inverse
14
+ getinverse <- function () inv
15
+ list (set = set , get = get ,
16
+ setinverse = setinverse ,
17
+ getinverse = getinverse )
8
18
}
9
19
10
20
11
21
# # Write a short comment describing this function
12
22
13
23
cacheSolve <- function (x , ... ) {
14
24
# # Return a matrix that is the inverse of 'x'
15
- }
25
+ inv <- x $ getinverse()
26
+ if (! is.null(inv )) {
27
+ message(" getting cached data" )
28
+ return (inv )
29
+ }
30
+ data <- x $ get()
31
+ inv <- solve(data )
32
+ x $ setinverse(inv )
33
+ inv
34
+ }
You can’t perform that action at this time.
0 commit comments