Skip to content

Commit 8db16b8

Browse files
committed
init repo
1 parent 7f657dd commit 8db16b8

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
ProgrammingAssignment2.Rproj

cachematrix.R

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions reduce the computation needed to find
2+
## the inverse of a matrix by caching the inverse the 1st
3+
## is it calculated
34

4-
## Write a short comment describing this function
5+
## cache the inverse of x and provide methods for
6+
## setting and getting that inverse
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
i <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
i <<- NULL
13+
}
14+
get <- function() x
15+
setinverse <- function(solve) i <<- solve
16+
getinverse <- function() i
17+
list(set = set, get = get,
18+
setinverse = setinverse,
19+
getinverse = getinverse)
820
}
921

10-
11-
## Write a short comment describing this function
22+
## get the inverse of x, using getinverse to get
23+
## it from cache or setinverse to cache it
1224

1325
cacheSolve <- function(x, ...) {
26+
i <- x$getinverse()
27+
if(!is.null(i)) {
28+
message("getting cached data")
29+
return(i)
30+
}
31+
data <- x$get()
32+
i <- solve(data, ...)
33+
x$setinverse(i)
1434
## Return a matrix that is the inverse of 'x'
35+
message("getting data")
36+
return(i)
1537
}
38+
39+
## testing data
40+
41+
y = matrix(c(1,2, 6,8), nrow=2, ncol=2)
42+
x <- makeCacheMatrix(y)

0 commit comments

Comments
 (0)