Skip to content

Commit 2e577e3

Browse files
author
Manoj Xavier
committed
Added completed cachematrix.R
1 parent 7f657dd commit 2e577e3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cachematrix.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7+
inv <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
inv <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(solve) inv <<- solve
14+
getinverse <- function() inv
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
718

819
}
920

@@ -12,4 +23,17 @@ makeCacheMatrix <- function(x = matrix()) {
1223

1324
cacheSolve <- function(x, ...) {
1425
## Return a matrix that is the inverse of 'x'
26+
inv <- x$getinverse()
27+
if(!is.null(inv)) {
28+
message("getting cached data")
29+
return(inv)
30+
}
31+
data <- x$get()
32+
inv <- solve(data, ...)
33+
x$setinverse(inv)
34+
inv
1535
}
36+
37+
a = matrix( c(4,10,23,12), nrow=2, ncol=2)
38+
mat <- makeCacheMatrix(a)
39+
cacheSolve(mat)

0 commit comments

Comments
 (0)