Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e32725e

Browse files
committed
matrix solution
1 parent 7f657dd commit e32725e

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

cachematrix.R

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
1+
## Put comments here that give an overall description of what your
2+
## functions do
3+
4+
## Write a short comment describing this function
5+
6+
makeCacheMatrix <- function(x = matrix()) {
7+
m <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(solve) m <<- solve
14+
getinverse <- function() m
15+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
16+
17+
}
18+
19+
20+
## Write a short comment describing this function
21+
22+
cacheSolve <- function(x, ...) {
23+
m <- x$getinverse()
24+
if(!is.null(m)) {
25+
message ("getting cached inverse matrix")
26+
return(m)
27+
}
28+
message("Don't have computation cached. Calculating inverse of input matrix...")
29+
data <- x$get()
30+
m <- solve(data)
31+
x$setinverse(m)
32+
m
33+
}
34+
35+
a <- matrix(c(1:9), nrow= 3, ncol = 3)
36+
a[[2,2]] <- 0
37+
a
38+
b <- makeCacheMatrix(a)
39+
cacheSolve(b)
40+

0 commit comments

Comments
 (0)