-
Notifications
You must be signed in to change notification settings - Fork 144k
Open
Description
makeCacheMatrix <- function(x = matrix()) {
-
inv <- NULL
-
set <- function(y) {
-
x <<- y
-
inv <<- NULL
-
}
-
get <- function() x
-
setInverse <- function() inv <<- solve(x) #calculate the inverse
-
getInverse <- function() inv
-
list(set = set,
-
get = get,
-
setInverse = setInverse,
-
getInverse = getInverse)
- }
cacheSolve <- function(x, ...) {
-
## Return a matrix that is the inverse of 'x'
-
inv <- x$getInverse()
-
if (!is.null(inv)) {
-
message("getting cached data")
-
return(inv)
-
}
-
mat <- x$get()
-
inv <- solve(mat, ...)
-
x$setInverse(inv)
-
inv
- }
funs <- makeCacheMatrix()
funs$set(matrix(1:4, 2))
funs$get()
[,1] [,2]
[1,] 1 3
[2,] 2 4
funs$setInverse()
funs$getInverse()
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5
Activity
jingxuanxiao commentedon Aug 20, 2018
I really like the fact that test samples are giving in the end.
Also the variable name as 'inv' is very self explanatory.
Well done.
Hemant0509 commentedon Sep 18, 2018
The logic is quite clear, well done!