Skip to content

Commit df9f380

Browse files
committed
add function
get inverse of a maxtric
1 parent 7f657dd commit df9f380

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

cachematrix.R

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Caching the Inverse of a Matrix
32

4-
## Write a short comment describing this function
3+
## This function creates a special "matrix" object that can cache its inverse
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
s <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
s <<- NULL
10+
}
11+
get <- function() x
12+
setsolve <- function(solve) s <<- solve
13+
getsolve <- function() s
14+
list(set = set, get = get,
15+
setsolve = setsolve,
16+
getsolve = getsolve)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
1221

1322
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
23+
s <- x$getsolve()
24+
if(!is.null(s)) {
25+
message("getting cached data")
26+
return(s)
27+
}
28+
data <- x$get()
29+
s <- solve(data, ...)
30+
x$setsolve(s)
31+
s
1532
}

0 commit comments

Comments
 (0)