Skip to content

Commit 1e84200

Browse files
authored
Update cachematrix.R
1 parent 7f657dd commit 1e84200

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

cachematrix.R

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,39 @@
33

44
## Write a short comment describing this function
55

6+
## This function will create a matrix function that allows user to set their matrix ($set), read the matrix($get)
7+
## This function will also allow user to read the inverse of function ($getinv)
8+
## If the result is NULL, then user need to run the cacheSolve() function
69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
m <- NULL
11+
set <- function(y) {
12+
x <<- y
13+
m <<- NULL
14+
}
15+
get <- function() x
16+
setinv <- function(solve) m <<- solve
17+
getinv <- function() m
18+
list(set = set, get = get,
19+
setinv = setinv,
20+
getinv = getinv)
821
}
922

10-
1123
## Write a short comment describing this function
1224

25+
## If makeCacheMatrix$getinv() is NULL, then user need to run the following function
26+
## This function will compute the inverse of matrix and store the result to the function that was
27+
## previously created in makeCachematric()
28+
29+
1330
cacheSolve <- function(x, ...) {
1431
## Return a matrix that is the inverse of 'x'
32+
m <- x$getinv()
33+
if(!is.null(m)) {
34+
message("getting cached data")
35+
return(m)
36+
}
37+
data <- x$get()
38+
m <- solve(data, ...)
39+
x$setinv(m)
40+
m
1541
}

0 commit comments

Comments
 (0)