Skip to content

Commit 1e7b11b

Browse files
author
AndyH
committed
adding methods to solve hw2
1 parent 7f657dd commit 1e7b11b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cachematrix.R

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,34 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
invert_x <- NULL
8+
9+
set <- function(y) {
10+
x <<- y
11+
invert_x <<- NULL
12+
}
13+
14+
get <- function() x
15+
setinverted <- function(inverted) invert_x <<- inverted
16+
getinverted <- function() invert_x
17+
18+
list(set = set, get = get,setinverted = setinverted,getinverted = getinverted)
819
}
920

1021

1122
## Write a short comment describing this function
1223

1324
cacheSolve <- function(x, ...) {
1425
## Return a matrix that is the inverse of 'x'
26+
inverted <- x$getinverted()
27+
28+
if(!is.null(inverted)) {
29+
message("Getting Cached Data")
30+
return(inverted)
31+
}
32+
norm <- x$get()
33+
inverted <- solve(norm)
34+
x$setinverted(inverted)
35+
36+
inverted
1537
}

0 commit comments

Comments
 (0)