Skip to content

Commit 4b8d911

Browse files
committed
both functions are finished
1 parent 7f657dd commit 4b8d911

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

cachematrix.R

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
11
## Put comments here that give an overall description of what your
22
## functions do
3+
## function makeCacheMatrix() creates a list of functions
4+
## that stored the original matrix and the invese matrix if it has been calculated
5+
## function cacheSolve() calculates the inverse matrix
36

47
## Write a short comment describing this function
8+
## this function returns a list which:
9+
## set the value of the original matrix
10+
## get the value of the original matrix
11+
## set the value of the inverse matrix
12+
## get the value of the inverse matrix
513

614
makeCacheMatrix <- function(x = matrix()) {
7-
15+
inversem <- NULL
16+
17+
set <- function(y) {
18+
x <<- y
19+
inversem <<- NULL
20+
}
21+
22+
get <- function() x
23+
24+
calinverse <- function(inverse) inversem <<- inverse
25+
26+
getinverse <- function() inversem
27+
28+
list(set = set, get = get,
29+
calinverse = calinverse,
30+
getinverse = getinverse)
831
}
932

1033

1134
## Write a short comment describing this function
35+
## calculate the inverse matrix and cache it
36+
## and it assumes that matrix x is invertible
1237

1338
cacheSolve <- function(x, ...) {
1439
## Return a matrix that is the inverse of 'x'
40+
inversem <- x$getinverse()
41+
if (!is.null(inversem)) {
42+
message("getting cached data")
43+
return(inversem)
44+
}
45+
data <- x$get()
46+
inverse <- solve(data)
47+
inversem <- x$calinverse(inverse)
48+
inversem
1549
}

0 commit comments

Comments
 (0)