Skip to content

Commit ff96de2

Browse files
author
Alexey Vorobiev
committed
initial commit of makeCacheMatrix and cacheSolve
1 parent 7f657dd commit ff96de2

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

cachematrix.R

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
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-
1+
## The first function, makeCacheMatrix creates a special "matrix",
2+
## which is really a list containing a function to:
3+
## set the value of the matrix
4+
## get the value of the matrix
5+
## set the value of the inverse matrix
6+
## get the value of the inverse matrix
67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
im <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
inverseMatrix <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(inverseMatrix) im <<- inverseMatrix
15+
getinverse <- function() im
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
819
}
920

1021

11-
## Write a short comment describing this function
22+
## The second function calulates inverse matrix, that is stored
23+
## in object function above, or returns cached inverse of the matrix
1224

1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
## Return a matrix that is the inverse of 'x'
27+
im <- x$getinverse()
28+
if(!is.null(im)) {
29+
message("getting cached matrix")
30+
return(im)
31+
}
32+
data <- x$get()
33+
im <- solve(data)
34+
x$setinverse(im)
35+
im
1536
}

0 commit comments

Comments
 (0)