Skip to content

Commit 0ccbdf9

Browse files
author
Sebastian Bassi
committed
ex2
1 parent 7f657dd commit 0ccbdf9

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

cachematrix.R

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
31

4-
## Write a short comment describing this function
2+
3+
## This function returns a list with 4 functions, set, get, setinv and getinv.
54

65
makeCacheMatrix <- function(x = matrix()) {
6+
m <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
m <<- NULL
10+
}
11+
get <- function() x
12+
setinv <- function(inv) m <<- inv
13+
getinv <- function() m
14+
list(set = set, get = get,
15+
setinv = setinv,
16+
getinv = getinv)
717

818
}
919

1020

11-
## Write a short comment describing this function
12-
21+
## this function, given the object produced by makeCacheMatrix, returns the inverse of a matrix
1322
cacheSolve <- function(x, ...) {
1423
## Return a matrix that is the inverse of 'x'
24+
m <- x$getinv()
25+
if(!is.null(m)) {
26+
message("getting cached data")
27+
return(m)
28+
}
29+
data <- x$get()
30+
m <- solve(data, ...)
31+
x$setinv(m)
32+
m
33+
34+
1535
}

0 commit comments

Comments
 (0)