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

Skip to content

Commit b2e4cae

Browse files
committed
implementation of makeCacheMatrix and cacheSolve functions with comments
1 parent 7f657dd commit b2e4cae

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

cachematrix.R

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
4+
## Takes matrix and returns special matrix which is list containing functions:
5+
## get() - returns initial matrix
6+
## set(y) - overwrites initial matrix with new one
7+
## getsolved() - returns cached inverse of initial matrix
8+
## setsolved() - caches inverse of matrix
59

610
makeCacheMatrix <- function(x = matrix()) {
7-
11+
s <- NULL
12+
13+
get <- function() x
14+
set <- function(y) {
15+
x <<- y
16+
s <<- NULL
17+
}
18+
19+
getsolved <- function() s
20+
setsolved <- function(solved) s <<- solved
21+
22+
list(get = get,
23+
set = set,
24+
getsolved = getsolved,
25+
setsolved = setsolved)
826
}
927

10-
11-
## Write a short comment describing this function
28+
## Takes special matrix and checks if it has cached inverse of matrix.
29+
## If not it computes inverse of matrix and stores it in cache.
30+
## It returns inverse of matrix as a result
1231

1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
solved <- x$getsolved()
34+
35+
if (is.null(solved)) {
36+
matrix <- x$get()
37+
solved <- solve(matrix, ...)
38+
x$setsolved(solved)
39+
}
40+
41+
solved
1542
}

0 commit comments

Comments
 (0)