Skip to content

Commit 933a6d3

Browse files
author
Beres Botond
committed
First version of the functions
1 parent 7f657dd commit 933a6d3

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

cachematrix.R

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

4-
## Write a short comment describing this function
5-
4+
# This function creates a special matrix which is really
5+
# a list containing a function to
6+
# 1. set the value of the matrix
7+
# 2. get the value of the matrix
8+
# 3. set the value of the inverse
9+
# 4. get the value of the inverse
610
makeCacheMatrix <- function(x = matrix()) {
7-
11+
inv <- NULL
12+
set <- function(y) {
13+
x <<- y
14+
inv <<- NULL
15+
}
16+
get <- function() x
17+
setinverse <- function(inverse) inv <<- inverse
18+
getinverse <- function() inv
19+
list(set=set, get=get, setinverse=setinverse,
20+
getinverse=getinverse)
821
}
922

1023

11-
## Write a short comment describing this function
12-
24+
# The following function calculates the inverse of the special "matrix"
25+
# created with makeCacheMatrix.
26+
# However, it first checks for an already cached inverse and skips
27+
# the computation if it finds one.
1328
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
29+
## Return a matrix that is the inverse of 'x'
30+
inv <= x$getinverse()
31+
if(!is.null(inv)) {
32+
message("getting cached data!")
33+
return(inv)
34+
}
35+
data <- x$get()
36+
inv <- solve(data, ...)
37+
x$setinverse(inv)
38+
inv
1539
}

0 commit comments

Comments
 (0)