Skip to content

Commit f9ef2cd

Browse files
committed
asst 2
1 parent 512b005 commit f9ef2cd

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

cachematrix.R

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
## and retrieving a matrix (set, get), as well as storing and retrieving
66
## the matrix's inverse (setinverse, getinverse).
77
makeCacheMatrix <- function(x = matrix()) {
8-
inv <- NULL
9-
set <- function(y) {
10-
x <<- y
11-
inv <<- NULL ## reset the inverse to null
12-
}
13-
get <- function() x
14-
setinverse <- function(inverse) inv <<- inverse
15-
getinverse <- function() inv
16-
list(set = set, get = get,
8+
inv <- NULL
9+
10+
set <- function(y) {
11+
x <<- y
12+
inv <<- NULL ## reset the inverse to null
13+
}
14+
get <- function() x
15+
16+
setinverse <- function(inverse) inv <<- inverse
17+
getinverse <- function() inv
18+
19+
list(set = set, get = get,
1720
setinverse = setinverse,
1821
getinverse = getinverse)
1922
}
@@ -25,14 +28,15 @@ makeCacheMatrix <- function(x = matrix()) {
2528
## - If a cached value does not exist, then the function calculates
2629
## the inverse, caches it, and returns that value.
2730
cacheSolve <- function(x, ...) {
28-
## Return a matrix that is the inverse of 'x'
29-
inv <- x$getinverse()
30-
if(!is.null(inv)) {
31-
message("getting cached data")
32-
return(inv)
33-
}
34-
data <- x$get()
35-
inv <- solve(data)
36-
x$setinverse(inv)
37-
inv
31+
inv <- x$getinverse()
32+
if(!is.null(inv)) {
33+
message("getting cached data")
34+
return(inv)
35+
}
36+
37+
data <- x$get()
38+
inv <- solve(data)
39+
x$setinverse(inv)
40+
41+
inv
3842
}

0 commit comments

Comments
 (0)