Skip to content

Commit 2c7685a

Browse files
committed
makeCacheMatrix and cacheSolve implemented.
1 parent 7f657dd commit 2c7685a

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

cachematrix.R

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
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+
## Takes a square matrix as argument and returns a list with functions
2+
## to solve the inverse and cache it to return it if was already called once;
63
makeCacheMatrix <- function(x = matrix()) {
7-
4+
m <- NULL
5+
set <- function(y) {
6+
x <<- y
7+
m <<- NULL
8+
}
9+
get <- function() x
10+
setinverse <- function(inverse) m <<- inverse
11+
getinverse <- function() m
12+
list(set = set, get = get,
13+
setinverse = setinverse,
14+
getinverse = getinverse)
815
}
916

1017

11-
## Write a short comment describing this function
12-
18+
## Takes a CacheMatrix list as argument and invokes the implemented
19+
## getInverse, which returns the inverted matrix, cached if it was already solved
20+
## called once or solves it and caches it for the next call;
1321
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
22+
m <- x$getinverse()
23+
if(!is.null(m)) {
24+
message("getting cached data")
25+
return(m)
26+
}
27+
data <- x$get()
28+
m <- solve(data)
29+
x$setinverse(m)
30+
m
1531
}

0 commit comments

Comments
 (0)