Skip to content

Commit ce6bc79

Browse files
committed
first version
1 parent e4eed41 commit ce6bc79

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

cachematrix.R

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Two functions that are used to create a special object
2+
## that stores a matrix and cache's its inverse
3+
##
34

4-
## Write a short comment describing this function
5+
## Function: makeCacheMatrix
6+
## Description: This function creates a special "matrix" object
7+
## that can cache it inverse
58

6-
makeCacheMatrix <- function(x = matrix()) {
9+
makeCacheMatrix <- function(matrix = matrix()) {
10+
#invMatrix - inverse matrix value
11+
invMatrix <- NULL #default value - NULL
712

13+
#setMatrix function: matrix and reset invMatrix
14+
setMatrix <- function(y){
15+
matrix <<- y
16+
invMatrix <<- NULL
17+
}
18+
#getMatrix function: get matrix
19+
getMatrix <- function() matrix
20+
21+
#setInvMatrix function: set inverse matrix
22+
setInvMatrix <- function(inverseMatrix) invMatrix <<- inverseMatrix
23+
24+
#getInvMatrix function: get inverse matrix
25+
getInvMatrix <- function() invMatrix
26+
27+
list(setMatrix = setMatrix, getMatrix=getMatrix,
28+
setInvMatrix = setInvMatrix,
29+
getInvMatrix = getInvMatrix)
830
}
931

1032

11-
## Write a short comment describing this function
33+
## Function: cacheSolve
34+
## Description: This function computes the inverse of the
35+
## special "matrix" returned by makeCacheMatrix
36+
37+
cacheSolve <- function(matrix, ...) {
38+
#get invMatrix
39+
invMatrix <- matrix$getInvMatrix()
40+
41+
#check if it is not null
42+
if (!is.null(invMatrix)){
43+
message("getting cached data")
44+
return (invMatrix)
45+
}
1246

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
47+
#if it is null, calculate the inverse
48+
data <- matrix$getMatrix()
49+
inv <- solve(data, ...)
50+
matrix$setInvMatrix(inv) #save it
51+
inv
1552
}

0 commit comments

Comments
 (0)